-2

Is there any chance to format a double value after an operation to come up with only 2 decimal values? Because i am making a weather report and i'm getting the average of weather data per 5 minutes and i want to make it formal to have only 2 decimal places.

Which instead of having 10240.8999 i could have 10240.90. Any thoughts or suggestion?

Solem
  • 17
  • 2
  • 7
  • There are dozens of duplicates on this site already. And you're not really clear whether you want to _round_ or _format_ a number. – H H Jan 02 '15 at 16:40
  • sorry about the confusion :" i am more referring to rounding up. ill change it – Solem Jan 02 '15 at 16:52

2 Answers2

0

In .NET, the way to do this is:

var rounded = Math.Round(valueToRound, 2);

for decimal variables:

var rounded = Decimal.Round(valueToRound, 2);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
-1

Round with 2 decimals and convert to string to force 2 decimals

string rounded = String.Format("{0:f2}", Math.Round(10240.8999, 2));

Gives 10240.90

Flat Eric
  • 7,971
  • 9
  • 36
  • 45