Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.Round not keeping the trailing zero

Tags:

c#

rounding

I need all values to rounded to two decimal places. So 1.401 should round to 1.40, but Math.Round(value, 2) rounds to 1.4.

How can I force the trailing zero?

like image 351
Blade3 Avatar asked Sep 06 '12 20:09

Blade3


People also ask

How do you get rid of trailing zeros in decimal?

You can remove trailing zeros using TRIM() function.

Is a trailing 0 acceptable?

In addition, a whole number should never be followed by a decimal point and a zero. These "trailing zeros" (e.g., 3.0) are a frequent cause of 10-fold overdoses and should never be used.

Why are trailing zeros important?

Trailing zeros (the right most zeros) are significant when there is a decimal point in the number. For this reason it is important to give consideration to when a decimal point is used and to keep the trailing zeros to indicate the actual number of significant figures.

What is the rule for trailing zeros?

To determine the number of significant figures in a number use the following 3 rules: Non-zero digits are always significant. Any zeros between two significant digits are significant. A final zero or trailing zeros in the decimal portion ONLY are significant.


3 Answers

The trailing zero is more of a formatting than a value issue, so use

foo.ToString("0.00")
like image 95
Joey Avatar answered Sep 19 '22 08:09

Joey


1.4 is the same as 1.40 - you just want to display it differently. Use a format string when calling ToString - like value.ToString("0.00")

like image 30
Paul Phillips Avatar answered Oct 16 '22 10:10

Paul Phillips


1.4 == 1.40 the only time you'd ever need a trailing 0 is when you display the number..i.e. format it to string.

.ToString("N2");
like image 14
Stan R. Avatar answered Oct 16 '22 09:10

Stan R.