Flea#

Hello,

I have a problem when calculating money with the decimal variable rounding up when I convert it over to a string and I am not sure why it is doing this.

For example, I have a value of 13.525 (SalesTaxTotal) which I am wanting to convert to a string.

Code Snippet

lblTax.Text = string.Format("{0:C}", SalesTaxTotal);

Once the line of code is executed, the value returned in lblTax.text is $13.53, so for whatever reason the string.Format is rounding the value up and of course I don't want it to do that.

Even if I hard code the value 13.525M into the method it will still return $13.53

Code Snippet
lblTax.Text =
string.Format("{0:C}", 13.525M);

Is there any explanation why it is doing this and what I can do to prevent it I simply want it to return $13.52.

Thanks,

Flea#




Re: .NET Base Class Library Decimal variable rounding when formatting as string

Matthew Watson

Put the number of decimal places you want after the C, e.g. for 4 decimal places do:

lblTax.Text = string.Format("{0:C4}", 13.525M);

Edit: oops sorry misread you. You want it to not round to the nearest value. But why would you want that It's less accurate!

If you really want to truncate the digits, you have to multiply the value by 100, take the integer part, and divide by 100:

Decimal d = 13.525M;
d = (int)(100 * d)/100M;

As to why 13.525 gets rounded to 13.53 when expressed to 2 decimal places, it's the standard way of rounding numbers:

http://en.wikipedia.org/wiki/Rounding




Re: .NET Base Class Library Decimal variable rounding when formatting as string

IsshouFuuraibou

it is a property of the C formatting, it rounds to the nearest cent, use custom formatting if you need to keep showing the full value, or up to a certain decimal point.

Code Snippet

"${0:#0.00}"



or better yet, let decimal fall to ToString to generate the right value

Code Snippet

"${0}"


Standard Number Formatting
Custom Number Formatting

Don't forget that custom formatting is available and rather useful at times





Re: .NET Base Class Library Decimal variable rounding when formatting as string

Flea#

Hi Matthew,

Well I guess that is my question. When dealing with money in every day life, are the values suppose to be rounded


For example, when string.format({"0:C"}, 13.525) returns $13.53, I am assuming it is doing that because that is how it is suppose to be

I only ask because I am checking my calculations on a calculator and of course, its returning the precise number and my program was returning one cent higher, so I just want to make sure I am returning the correct value in my program.


Thanks,

Flea






Re: .NET Base Class Library Decimal variable rounding when formatting as string

Matthew Watson

I think you want to display more digits of precision. By default, you only get 2.

Increase it to 4 or more (by putting that number after "C", as I showed earlier).




Re: .NET Base Class Library Decimal variable rounding when formatting as string

Flea#

Thanks for your help! I took my values into Excel and ran them through there and I was getting the same results as my C# was, so I will just leave it as is since Excel is doing the same thing!


Thanks again!

Flea