Nicke Manarin
Nicke Manarin

Reputation: 3360

Formatting double

I have this big number (a double): 123456789012345.012345

That should be represented as: 123.456.789.012.345,01 using the pt-BR format.

This is the format {0:###,###,###,###,##0.00} that I'm using, yet this is the result:

enter image description here

As you can see, after 13 characters, the format starts to round the decimal places. How can we display correctly the number without rounding?

Upvotes: 0

Views: 65

Answers (2)

RobertPeric
RobertPeric

Reputation: 51

decimal numb = 123456789012345.012345m;
string strNumb = string.Format(new CultureInfo("pt-BR"), "{0:###,###,###,###,##0.00}", numb);
string strNumb2 = string.Format(new CultureInfo("pt-BR"), "{0:###,###,###,###,##0.00}", 123456789012345.012345m);

You can use decimal instead of double.

Upvotes: 2

D Stanley
D Stanley

Reputation: 152624

double only has 15-16 digits of precision, so any significant digits after that are truncated.

decimal has 28-29 digits of precision, and should be used if you need to represent the number as a decimal number without any loss of precision.

Upvotes: 3

Related Questions