Marc
Marc

Reputation: 2859

show decimal or double with zeros

i get price values from DB.

now whenever the price is perhaps 5, I want to show 5.00

if its 4.3 it should be 4.30.

how to convert that?

thanks

Upvotes: 0

Views: 2548

Answers (4)

ram
ram

Reputation: 11626

yourDecimal.ToString("N2") will also do the same

Upvotes: 1

Rik
Rik

Reputation: 29243

What data types do you use to store the price? It's a bad idea to store prices using floating point numbers because of precision issues. A fixed point number like a decimal is a better idea.

Once you're settled on a data type, you can use string formatting to display it correctly. See MSDN.

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166476

You can use the string format for decimal to apply this formatting.

YourDecimal.ToString("#,##0.00");

this should show 5.00, and 4.30.

Also it will show 1,234.56 groupings.

Upvotes: 3

Related Questions