By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

Conversions between float and string

P: n/a
Values altered when I convert from string to float and back again:

using System;

class FloatTest
{
static void Main(string[] args)
{
string var1 = "800856.22";
Console.WriteLine(var1);
float var2 = Convert.ToSingle(var1.Replace(".", ","));
Console.WriteLine(var2.ToString());
}
}

My output:

800856.22
800856,3

The input data use period as decimal point, while the computers that
will run this program use comma (due to the regional settings in
Windows). That's why I do the Replace(). I'm amazed that the
ToSingle() method doesn't handle period and comma equivalently. How
would I do if I needed to distribute the program internationally?

The big problem is the alteraion of value. The program handles account
balances, so there must be no alterations like this. What am I doing
wrong?

Gustaf

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Gustaf Liljegren <gu**************@bredband.net> wrote:
Values altered when I convert from string to float and back again:

using System;

class FloatTest
{
static void Main(string[] args)
{
string var1 = "800856.22";
Console.WriteLine(var1);
float var2 = Convert.ToSingle(var1.Replace(".", ","));
Console.WriteLine(var2.ToString());
}
}

My output:

800856.22
800856,3

The input data use period as decimal point, while the computers that
will run this program use comma (due to the regional settings in
Windows). That's why I do the Replace(). I'm amazed that the
ToSingle() method doesn't handle period and comma equivalently. How
would I do if I needed to distribute the program internationally?
You'd use the versions of Single.Parse and Single.ToString which take
an IFormatProvider, and pass the appropriate CultureInfo for what you
expect to receive/want to produce.
The big problem is the alteraion of value. The program handles account
balances, so there must be no alterations like this. What am I doing
wrong?


You're expecting floats to carry more information than they do. If you
do:

float f = 800856.22f;
Console.WriteLine (f);

you'll get the same error. In fact, the single that is produced is
exactly 800856.25, but the formatting is rounding off to 800856.3. (If
you want to see exact values for singles/doubles, see
http://www.pobox.com/~skeet/csharp/floatingpoint.html and look for
DoubleConverter.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Hello,

There are two possible problems here. Firstly, the value 800856.22 may be
too precise to be held in a Single, so is being rounded to something else.
Or, the value may be held correctly, but is being rounded to 7 signifcant
digits when converted to a string - in other words, "800856,3" may not be
the actual value of var2.

I would use Doubles instead, e.g
double var2 = Convert.ToDouble(var1);

I hope that helps.
"Gustaf Liljegren" <gu**************@bredband.net> wrote in message
news:up*************@TK2MSFTNGP12.phx.gbl...
Values altered when I convert from string to float and back again:

using System;

class FloatTest
{
static void Main(string[] args)
{
string var1 = "800856.22";
Console.WriteLine(var1);
float var2 = Convert.ToSingle(var1.Replace(".", ","));
Console.WriteLine(var2.ToString());
}
}

My output:

800856.22
800856,3

The input data use period as decimal point, while the computers that
will run this program use comma (due to the regional settings in
Windows). That's why I do the Replace(). I'm amazed that the
ToSingle() method doesn't handle period and comma equivalently. How
would I do if I needed to distribute the program internationally?

The big problem is the alteraion of value. The program handles account
balances, so there must be no alterations like this. What am I doing
wrong?

Gustaf

Nov 15 '05 #3

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote
Windows). That's why I do the Replace(). I'm amazed that the
ToSingle() method doesn't handle period and comma equivalently. How would I do if I needed to distribute the program internationally?
You'd use the versions of Single.Parse and Single.ToString which

take an IFormatProvider, and pass the appropriate CultureInfo for what you expect to receive/want to produce.


Thank you both. And excellent reading about floating-point numbers!
I've been looking for something like this article. Using decimal
instead of float solved my problem.

Using IFormatProvider, is there a way to make the program "sensitive"
to the computers region, so that these conversions never fail? I
wasn't able to figure out how to use the class.

Gustaf

Nov 15 '05 #4

P: n/a
Gustaf Liljegren <gu**************@bredband.net> wrote:
Thank you both. And excellent reading about floating-point numbers!
I've been looking for something like this article. Using decimal
instead of float solved my problem.
Thanks :)
Using IFormatProvider, is there a way to make the program "sensitive"
to the computers region, so that these conversions never fail? I
wasn't able to figure out how to use the class.


The normal thing to do is pass one of CultureInfo.CurrentUICulture,
CultureInfo.CurrentCulture or CultureInfo.InvariantCulture - but I
don't think there's a culture which allows *both* in the parsing, if
that's what you were after.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
Use the decimal type instead. It provides the level of fractional precision
that you require.

Also, use Parse instead to convert:

decimal var2 = decimal.Parse(var1);

Gustaf Liljegren wrote:

Values altered when I convert from string to float and back again:

using System;

class FloatTest
{
static void Main(string[] args)
{
string var1 = "800856.22";
Console.WriteLine(var1);
float var2 = Convert.ToSingle(var1.Replace(".", ","));
Console.WriteLine(var2.ToString());
}
}

My output:

800856.22
800856,3

The input data use period as decimal point, while the computers that
will run this program use comma (due to the regional settings in
Windows). That's why I do the Replace(). I'm amazed that the
ToSingle() method doesn't handle period and comma equivalently. How
would I do if I needed to distribute the program internationally?

The big problem is the alteraion of value. The program handles account
balances, so there must be no alterations like this. What am I doing
wrong?

Gustaf

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.