开发者

.Net numbers formatting

I've gone over all the numbers formatting options of .Net framework , and I cannot find the formatting I need.

I have a float variable for which I need a format string ( not code ) which does the following :

If value = 1500.00 the formatted string will be : 1,500 ( and not 1,500.00 ) If value = 1500.53 the formatted string will be : 1,500.53 If value = 0.53 the formatted string will be : 0.53

So that the "n" and "N0" formats do not do the trick for me, since "n" always adds ".00" if the value is whole number because "n" it's for float numbers, and "N0" round the number to who开发者_运维百科le number even if the value is not a whole number (1500.53)

I need this as a string since I use the formatting in ASPXGridView (devexpress control) columns - DisplayFormatString attribute

Anyone can help ?

Thanks

Another thing : the number of digits after the . can vary .. for example can be : 1500.53536


I don't have Visual Studio available right now to test it, but after reading the documentation I'm pretty sure that the following should work:

#,##0.##

EDIT: If your numbers can have more than 2 decimal places, just add an appropriate number of #s after the dot, e.g.:

#,##0.##############

# means: digit or empty, whereas 0 means: digit or 0. Thus, the example given will display only as many digits after the decimal point as necessary (but at most the number of #s).


Something like "###,##0.##" then?


Look at custom numeric format strings. I think you want something like this: "##,#.##".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜