Double to String with 2 decimals

Jelmer

Regular
Joined
Jan 11, 2006
Messages
96
Hello,

I've got some double's.
They are printed like this:

Code:
Math.Round(totaalBTW,2)

But they are not rounded into 2 decimals if they have no decimals.

totalBTW = 0;

then with Math.Round(totaalBTW,2) he is still 0.

So how can i print a double always with 2 decimals????
 
ToString("f2")

Jelmer said:
So how can i print a double always with 2 decimals????
Visual Basic:
Math.Round(totalBTW, 2).ToString("f2")

The "f2" parameter tells the ToString method to format the output as fixed with 2 decimal places.

Todd
 
Great!

Before C# i programmed in Delphi, then Math.Round(totalBTW, 2) was enough.
Thanks for the reply!
 
Maybe you know this problem also:

The result is:

1653,84

I like it to display as:

1.653,84

What code is responsible for that?
 
Try ToString("n2")

You can also look in the help for string formats. Every object supports the ToString method and many of the built in types support special overloads. Besides number formatting, date formatting comes up a lot and there are lots of options for printing things the right way. By printing, I mean conversion to strings :)

-ner
 
Back
Top