Answered by:
How to round off a float to 2 decimal places?

Hello,
float f = 1.345698;
I want f to be 1.34. Is there any C# methods like Math.Round that I can use? I tried Math.Round but it only accepts decimals and when I try to cast my float as a decimal I get an error saying the number is to large to cast as a decimal.
Thanks,
Rhonika
Question
Answers

If you want it to be 1.34, you'll need to truncate, not round, the value:
float f = 1.345698F; float truncated = (float)(Math.Truncate((double)f*100.0) / 100.0); float rounded = (float)(Math.Round((double)f, 2);
If you need a way to truncate to a specified number of digits, for floats, that's more general purpose, you could move this into an extension method:
public static float Truncate(this float value, int digits) { double mult = Math.Pow(10.0, digits); double result = Math.Truncate( mult * value ) / mult; return (float) result; }
Then you could just do:
float truncated = f.Truncate(2);
Reed Copsey, Jr.  http://reedcopsey.com Marked as answer by rrhonika Friday, December 18, 2009 8:39 PM
All replies

namespace ForumHelp { using System; class Program { static void Main(string[] args) { float foo = 3.14516f; decimal bar = Convert.ToDecimal(foo); bar = Math.Round(bar, 2); Console.WriteLine("Unrounded: {0}\nRounded {1}", foo, bar); Console.ReadLine(); } } }
Blog (WPF/C#)
Crystal (WPF Google Contact UI/C# Library) 
If you want it to be 1.34, you'll need to truncate, not round, the value:
float f = 1.345698F; float truncated = (float)(Math.Truncate((double)f*100.0) / 100.0); float rounded = (float)(Math.Round((double)f, 2);
If you need a way to truncate to a specified number of digits, for floats, that's more general purpose, you could move this into an extension method:
public static float Truncate(this float value, int digits) { double mult = Math.Pow(10.0, digits); double result = Math.Truncate( mult * value ) / mult; return (float) result; }
Then you could just do:
float truncated = f.Truncate(2);
Reed Copsey, Jr.  http://reedcopsey.com Marked as answer by rrhonika Friday, December 18, 2009 8:39 PM

float mynum = 1.345698f;
double r = Math.Floor(mynum * 100) / 100; Proposed as answer by Wesley McCammon Friday, December 18, 2009 8:27 PM


Rhonika,
You got plenty of options already specified but I wanted to throw this out. If you want to keep the precision in the variable and simply change what is displayed you could use string.Format like so:
float x = 1.345698f; string msg = String.Format("{0:0.00}", x);
Of course, this doesn't account for 1.34 instead of 1.35 which I will assume is a typo or that you meant to ask for truncation which has also been answered.
Michael Fischer

So I am just trying something like this today, and the solution presented here has a huge gaping problem...A float converted to a double is not always the same value, at least not in VS2010 in C#.
Given this code:
privatestringValidateContributionRate() { string result = null; var truncated = (float) (Math.Truncate(ContributionRate*10000.0)/10000.0); if ((ContributionRate  truncated) > 0) { result = "Level " + Name + ": Contribution Rate can only be 4 decimal places"; }
It turns out that 0.12f becomes a double value of 0.11999999731779099.
Multiply that by 10000 and divide again, you get .1199, instead of the expected .12
I don't suppose anyone has a solution to this conundrum?
