开发者

Trailing zero on decimal

I have a string which looks like this 512.3 is there a way to add a trailing zero so it looks like this 512.30

Just to clarify (sorry I didn't know there where different ways etc.)

My string is an amount which is passed so changes all the time I only need the trailing zero on amounts like 开发者_如何学编程512.3, 512.4,512.5 etc. as some of my amounts will pass values like 512.33 and 512.44 and so on

Thanks

Jamie


float.Parse("512.3").ToString("0.00");

It would give the number with two decimal digits.


You're going to want to use String.Format or some derivation thereof, and the format string will look like

myString = String.Format("{0:F2}",myObject);

Also note that format strings can be used in the .ToString("F2") method (notice I included the format string F2 inside there already.

See the MSDN links above for a more thorough and definitive explanation.


If it's VB.NET it seems like the simplest solution would be:

FormatNumber("512.3", 2)

Which would return 512.30


Format(5.12, "0.00") this will format it as two decimals.


You'll want to use String.Format

decimal your_number = 1452.66m;
string str = String.Format("{0:C}", your_number);


Single.Parse("512.3").ToString("0.00") ' VB Version

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜