C# Padding Amount With Zeros

坚强是说给别人听的谎言 提交于 2019-12-23 06:56:50

问题


I have an amount field which is a decimal in the database. I need to always display this amount with 10 numbers on the left of the decimal and two after.

Example:
Amount = 245.00 which should display as 0000000245.00 Additionally, the amount could be over 1,000 or 10,000 which should display as:

0000001245.00 and 0000011245.00

How can I format the amount to always have the appropriate number of zeros on the left side of the decimal with a variable amount size?


回答1:


You should put 0's in your format string. Something like this.

myValue.ToString("0000000000.00");

That will always give you 10 digits on the left side of your decimal point and two on the right.

If you do not want two digits on the right side... do this.

myValue.ToString("0000000000.##");

This says hey if we have a value display it; otherwise, skip it.




回答2:


This should help... http://www.csharp-examples.net/string-format-int/

So you can use this

string.Format("{0:0000000000.00}", 15.25); // "0000000015.25"




回答3:


yourDecimalVariable.ToString("0000000000.00") should do the trick.



来源:https://stackoverflow.com/questions/9587819/c-sharp-padding-amount-with-zeros

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