single to string problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ibiza

    #1

    single to string problem

    Hi,

    can someone help me with that, it's driving me nuts...
    -----
    dim s as single = 42.5
    dim msg as string = s.tostring()
    ' now msg is supposed to hold 42.5 but NO it's rather containing 43
    -----
    It seems the tostring() function rounds the number but I don't
    understand why it does...

    thanks,
    ibiza

  • jayeldee

    #2
    Re: single to string problem

    ibiza,
    Bizarre. I copy/pasted your code into a new console app (vb.net 2003)
    and it worked fine. Have you tried overloading the ToString( ) with a
    specific format string? Trying using s.tostring("r") and see if the
    output is the same.

    Comment

    • ibiza

      #3
      Re: single to string problem

      wow...with the "r" format string it seems to work ok
      thank you so much!

      Anyways, I wonder why this time the functions rounds the single...it
      usually does NOT O_o
      and what's the "r" for?


      THANKS again! :)
      ibiza

      Comment

      • jayeldee

        #4
        Re: single to string problem

        ibiza,

        By default, ToString should return 7 digits of precision but it sounds
        like it isn't doing that in your code. Passing it "r" or "R" in the
        ToString method does this..."returns 7 digits if the number can be
        represented with that precision or 9 digits if the number can only be
        represented with maximum precision." - from the MSDN information on
        Single.

        Comment

        Working...