20

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

string strTemp = "0.51667308807373";

convert to decimal by rounding of two decimal places.

Community
  • 1
  • 1
Sankar M
  • 4,549
  • 12
  • 37
  • 55
  • 2
    What have you tried? What didn't work? Please post your code and explain where you are having difficulties. – Oded Aug 16 '11 at 10:37
  • Do you want the number stored to be rounded or do you just want to round it when you display? ie should the decimal variable be storing "0.51667308807373" and showing "0.52" or should it be storing "0.52"? – Chris Aug 16 '11 at 10:41
  • @Binary Worrier: this question is more about parsing than formatting I think... – Chris Aug 16 '11 at 10:42

6 Answers6

43
Math.Round(Convert.ToDecimal(strTemp), 2);
CrazyMPh
  • 580
  • 1
  • 5
  • 7
20

First convert string to decimal (Using Decimal.Parse or Decimal.TryParse).

decimal d = Decimal.Parse("123.45678");

Then round the decimal value using Round(d, m) where d is your number, m is the number of decimals, see http://msdn.microsoft.com/en-us/library/6be1edhb.aspx

decimal rounded = Decimal.Round(d, 2); 

If you only want to round for presentation, you can skip rounding to a decimal and instead simply round the value in output:

string.Format("{0:0.00}", 123.45678m);  
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
6

Convert the value to a floating point number, then round it:

double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
temp = Math.Round(temp, 2);

Alternatively, if you want the result as a string, just parse it and format it to two decimal places:

double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
string result = temp.ToString("N2", CultureInfo.InvariantCulture);

Note: The CultureInfo object is so that the methods will always use a period as decimal separator, regardless of the local culture settings.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0
var roundedTemp = Math.Round(decimal.Parse(strTemp), 2);

You might want to check to ensure the string is always a decimal first but think this is the essence of it.

dove
  • 20,469
  • 14
  • 82
  • 108
  • 3
    Since when does `Math.Round` take a string parameter? – Oded Aug 16 '11 at 10:39
  • i'd taken a quick second take before your comment, it's hardly a paradigm leap to parse the string ;) – dove Aug 16 '11 at 10:41
  • It's hardly a good answer if it doesn't even compile. Do you think the OP would have been able to make the leap by himself? – Oded Aug 16 '11 at 10:41
  • and edited answer before i'd seen your comment and all – dove Aug 16 '11 at 10:42
  • Its not a paradigm leap but it is a core part of the correct answer. :) – Chris Aug 16 '11 at 10:43
  • @Chris you're not wrong and I should be more considered before firing in a quick answer. think Oded had a point asking for more specifics on the question and on what the OP was looking for. – dove Aug 16 '11 at 10:49
  • @dove: indeed. Anyway, you have a +1 from me now since your answer is now correct and it feels mean for you to be on a -1 due to an itchy trigger finger. :) – Chris Aug 16 '11 at 10:56
  • i need to be briefed...Is it work or fails? – Sankar M Aug 16 '11 at 10:57
  • @Chris ta, and think i'll pass on the magnanimity and +1 Anders. lesson learnt – dove Aug 16 '11 at 10:58
0

you can use info from this link http://www.csharp-examples.net/string-format-double/ for the double value, use double.parse api

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

You can use Number format Info. Something like

NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;


double myInt = 0.51667308807373;

// Displays the same value with four decimal digits.
nfi.NumberDecimalDigits = 2;
Console.WriteLine(myInt.ToString("N", nfi));
Console.ReadKey();