Is there a .Net equivalent of the FormatNumber function?
FormatNumber
Printable View
Is there a .Net equivalent of the FormatNumber function?
FormatNumber
What do you mean exactly ? the link you provided shows that FormatNumber IS available in VB.NET.
Isn't it a carryover from VB6?
More or less, yes. It works differently though.
The ToString() method of the numeric types is overloaded to take a format string. You can use Standard Numeric Format Strings to cover many cases, or Custom Numeric Format Strings for more cases. Composite Formatting is also a good read on this topic.
Let's walk through some examples. Don't pay attention to my syntax as much as the strings I pass to ToString(); you don't normally call methods on a literal and there's some syntax errors in the way I typed.
To get parenthesis and better control over leading zeroes, you have to use custom format strings:Code:123.4567.ToString("N2") -> 123.45
123.4.ToString("N2") -> 123.40
12345.ToString("N") -> 12,345.00
12345.ToString("G") -> 12345
12345.678.ToString("G2") -> 1.2E+0.4
12345.ToString("D8") -> 00012345
12345.54321.ToString("F3") -> 12345.543
0.2.ToString("G") => 0.2
There's some really funky things you can do with the custom specifiers, and I doubt the above was all clear. Have a look at the documentation and do some experiments on your own and it should become clear.Code:0.25.ToString("0.00") -> 0.250
.25.ToString(".000") -> .250
-321.123.ToString("###.###") -> -321.123
-321.123.ToString("###.####") -> -321.123
-321.123.ToString("#.###;(#.###)") -> (321.123)
1234567.ToString("#,#") -> 1,234,567
All that aside, there probably is a compatibility FormatNumber() method provided. Obviously people fight over whether this is OK in a .NET program or not. Here's where I stand. FormatNumber() is only going to be clear to a VB6 programmer. If someone started their life as a VB .NET programmer, they're going to have to look it up in the documentation, and I'd argue it's a slightly confusing function. On the other hand, all .NET programmers should be familiar with at least the standard formatting strings and won't stumble over them. I wouldn't use FormatNumber() in my code and would prefer the .NET formatting strings. If you're the only person that's ever going to work on your source (or if you disagree) feel free to use it.
Ah! now I see what you mean :D
As Sitten mentionned, the String.Format Method might be what you are looking for. Here are other examples, they're in C# but hey, its just syntax anyway.
Thanks guys ... I guess I should've known the answer to this as I've used the .ToString overloads before... :o