用零显示十进制或双精度

时间:2023-01-08 16:01:58

i get price values from DB.

我从DB获得价格值。

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

现在每当价格大概是5时,我想显示5.00

if its 4.3 it should be 4.30.

如果它的4.3应该是4.30。

how to convert that?

如何转换?

thanks

4 个解决方案

#1


3  

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

您可以使用十进制的字符串格式来应用此格式。

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

this should show 5.00, and 4.30.

这应该显示5.00和4.30。

Also it will show 1,234.56 groupings.

它还将显示1,234.56个分组。

#2


2  

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.

一旦确定了数据类型,就可以使用字符串格式正确显示它。请参阅MSDN。

#3


1  

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

yourDecimal.ToString(“N2”)也会这样做

#4


0  

I never wrote a single line in Asp.net but simple search in google gave me this :

我从来没有在Asp.net写过一行,但谷歌的简单搜索给了我这个:

http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=181 http://msdn.microsoft.com/en-us/library/dwhawy9k%28VS.71%29.aspx

#1


3  

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

您可以使用十进制的字符串格式来应用此格式。

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

this should show 5.00, and 4.30.

这应该显示5.00和4.30。

Also it will show 1,234.56 groupings.

它还将显示1,234.56个分组。

#2


2  

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.

一旦确定了数据类型,就可以使用字符串格式正确显示它。请参阅MSDN。

#3


1  

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

yourDecimal.ToString(“N2”)也会这样做

#4


0  

I never wrote a single line in Asp.net but simple search in google gave me this :

我从来没有在Asp.net写过一行,但谷歌的简单搜索给了我这个:

http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=181 http://msdn.microsoft.com/en-us/library/dwhawy9k%28VS.71%29.aspx