To display Number With 2 Decimal Places

不羁的心 提交于 2019-12-01 14:26:23

问题


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?


回答1:


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:


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:


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:

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

This approach will work on all numbers.




回答4:


can be accomplish like..

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



回答5:


dbNumber.ToString("N2")

where dbNumber is the variable to convert.



来源:https://stackoverflow.com/questions/1304132/to-display-number-with-2-decimal-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!