Results 1 to 6 of 6

Thread: FormatNumber function

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    FormatNumber function

    Is there a .Net equivalent of the FormatNumber function?

    FormatNumber

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: FormatNumber function

    What do you mean exactly ? the link you provided shows that FormatNumber IS available in VB.NET.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: FormatNumber function

    Isn't it a carryover from VB6?

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: FormatNumber function

    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.

    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
    To get parenthesis and better control over leading zeroes, you have to use custom format strings:
    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
    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.

    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.

  5. #5
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: FormatNumber function

    Quote Originally Posted by nbrege View Post
    Isn't it a carryover from VB6?
    Ah! now I see what you mean

    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.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: FormatNumber function

    Thanks guys ... I guess I should've known the answer to this as I've used the .ToString overloads before...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width