G
Gustaf Liljegren
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
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