slandau
slandau

Reputation: 24052

Math rounding in c# - whole numbers issue

<%= Math.Round(keyRate, 5) %>

So in our view, we use this to display a bunch of numbers. it seems when we have a whole number, it rounds to 2 decimals instead of 5. Is this expected behavior? If so, how would I change it.

Thanks!

Upvotes: 0

Views: 1083

Answers (3)

BrokenGlass
BrokenGlass

Reputation: 160892

The second parameter to Math.Round(d, decimals) is the number of fractional digits that will be returned at most. From MSDN:

If the precision of d is less than decimals, d is returned unchanged.

In your case you are passing a number with less fractional digits, so it remains unchanged.

Upvotes: 1

David
David

Reputation: 73564

There's a difference between what a number is and how it's displayed when written out. You may need to override ToString() with ToString(string)

Per the Custom Numeric Formats, if you want it to be 5 places, you would use

Math.Round(keyRate, 5).ToString(00.00000)

More information on the "0" custom specifier.

Upvotes: 1

Emond
Emond

Reputation: 50672

How do you know that it is rounding to 2 decimals while you are passing a whole number? My guess is that you are outputting only 2 decimals.

Upvotes: 0

Related Questions