Home All Groups Group Topic Archive Search About

single to string problem

Author
5 May 2006 9:40 PM
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

Author
5 May 2006 10:21 PM
jayeldee
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.
Author
6 May 2006 5:41 PM
ibiza
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
Author
6 May 2006 7:09 PM
jayeldee
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.