显示带有2个小数位数的数字

时间:2021-03-25 16:36:15

I have a field in datatable .If 1000 is the value in it, i want to display it as 1000.00.Then if user changes to 1000.50 it should display as it is.Is there anyway to do this?Can anybody help?

我在datatable中有一个字段。如果1000是其中的值,我想将其显示为1000.00。然后,如果用户更改为1000.50,它应该显示为原样。无论如何要做到这一点吗?有人可以帮忙吗?

5 个解决方案

#1


30  

Sample Code:

示例代码:

Dim bigNumber As Decimal = 1234567.123456
Console.WriteLine("F2: " & bigNumber.ToString("F2"))
Console.WriteLine("N2: " & bigNumber.ToString("N2"))

Output:

输出:

F2: 1234567.12
N2: 1,234,567.12

#2


5  

There is a good chance that you want to display currency, so do this:

您很有可能想要显示货币,所以这样做:

1000m.ToString("C"); // Will show $1000.00, $1000,00 etc depending on culture
// OR just
1000m.ToString("N2"); 1000m.ToString("F2"); // For plain numbers: 1000.00, 1000,00

#3


3  

dbNumber.ToString("N2")

where dbNumber is the variable to convert.

其中dbNumber是要转换的变量。

#4


3  

can be accomplish like..

可以完成...

decimal ab = 50;
ab.ToString("####0.00");

#5


3  

Just ToString will not necessarily work in all situations. If you were to format the decimal fields of a data row, the following would be required:

Just ToString不一定适用于所有情况。如果要格式化数据行的十进制字段,则需要以下内容:

Format(datarow("field"), "C") ' for currency
Format(datarow("field"), "N2") ' for 2 decimal places

This approach will work on all numbers.

这种方法适用于所有数字。

#1


30  

Sample Code:

示例代码:

Dim bigNumber As Decimal = 1234567.123456
Console.WriteLine("F2: " & bigNumber.ToString("F2"))
Console.WriteLine("N2: " & bigNumber.ToString("N2"))

Output:

输出:

F2: 1234567.12
N2: 1,234,567.12

#2


5  

There is a good chance that you want to display currency, so do this:

您很有可能想要显示货币,所以这样做:

1000m.ToString("C"); // Will show $1000.00, $1000,00 etc depending on culture
// OR just
1000m.ToString("N2"); 1000m.ToString("F2"); // For plain numbers: 1000.00, 1000,00

#3


3  

dbNumber.ToString("N2")

where dbNumber is the variable to convert.

其中dbNumber是要转换的变量。

#4


3  

can be accomplish like..

可以完成...

decimal ab = 50;
ab.ToString("####0.00");

#5


3  

Just ToString will not necessarily work in all situations. If you were to format the decimal fields of a data row, the following would be required:

Just ToString不一定适用于所有情况。如果要格式化数据行的十进制字段,则需要以下内容:

Format(datarow("field"), "C") ' for currency
Format(datarow("field"), "N2") ' for 2 decimal places

This approach will work on all numbers.

这种方法适用于所有数字。