single to string problem

  • Thread starter Thread starter ibiza
  • Start date Start date
I

ibiza

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
 
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.
 
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
 
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.
 
Back
Top