string conversion to double
-
how to convert string like 0.5555 to rounded double value of 0.6 in this case? Because when i use Convert.ToDouble("0.555555") the result is 55555555.. Thanks
Hi, the Convert class (or the Parse methods of several classes) will turn a string into a numeric value that matches the string as good as it can. There is no way around that. You can change the result by adding another statement that operates on the numeric variable, something like
double d=Math.Round(d,1);
. AFAIK there isn't a single statement that will do both at once. read up on the Math.Round() method. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
-
Hi, the Convert class (or the Parse methods of several classes) will turn a string into a numeric value that matches the string as good as it can. There is no way around that. You can change the result by adding another statement that operates on the numeric variable, something like
double d=Math.Round(d,1);
. AFAIK there isn't a single statement that will do both at once. read up on the Math.Round() method. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.
-
Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.
Aljaz111 wrote:
you can't convert string 0.555 to double value 0.555
Of course you can, just use double.Parse or better still TryParse
double d = double.Parse("0.555");
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.
Aljaz111 wrote:
Always removes 0 from first place..
Is the decimal separator in your country by any chance a comma? How about this for verification;
string s = "0.55555";
double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
string s2 = d.ToString("#,#0.0");
Console.WriteLine(s2);I are Troll :suss:
-
how to convert string like 0.5555 to rounded double value of 0.6 in this case? Because when i use Convert.ToDouble("0.555555") the result is 55555555.. Thanks
I'm not quite sure how you're approching this but it's quite simple
public static double StringToRoundedDouble(string s, int decimals)
{
double result;
if (double.TryParse(s, out result))
result = Math.Round(result, decimals, MidpointRounding.AwayFromZero);
return result;
}Useage
double d =(StringToRoundedDouble("0.5555", 1);
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Aljaz111 wrote:
Always removes 0 from first place..
Is the decimal separator in your country by any chance a comma? How about this for verification;
string s = "0.55555";
double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
string s2 = d.ToString("#,#0.0");
Console.WriteLine(s2);I are Troll :suss:
-
Aljaz111 wrote:
Always removes 0 from first place..
Is the decimal separator in your country by any chance a comma? How about this for verification;
string s = "0.55555";
double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
string s2 = d.ToString("#,#0.0");
Console.WriteLine(s2);I are Troll :suss:
Good thinking :thumbsup:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Good thinking :thumbsup:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Ok it doesn't work without System.Globalization.CultureInfo.InvariantCulture but with it in Convert.Double(s, System.Globalization.CultureInfo.InvariantCulture) works just fine. Can you explain me please what does this have to do with rounding numbers? Thx
-
Ok it doesn't work without System.Globalization.CultureInfo.InvariantCulture but with it in Convert.Double(s, System.Globalization.CultureInfo.InvariantCulture) works just fine. Can you explain me please what does this have to do with rounding numbers? Thx
It interprets the decimal point wrong, if you replace the point with a comma then all goes well. There's also a
System.Globalization.CultureInfo.CurrentCulture
. The invariant culture will use the decimal-point, no matter what's been set currently by the user. If you convert the double "2.0" from invariant to your CurrentCulture[^], then you'd probably get "2,0" :)I are Troll :suss: