Hello,
I have this string of the value 0.5 and I need to convert it into a double but what I get is "5". I suspect that it is a culture problem. In Italy the dot is used to separate thousands and the comma to indicate fractions. How can can I do the conversion in an italian system but with an English result?
Conversion string ->double
Page 1 of 110 Replies - 592 Views - Last Post: 01 October 2012 - 08:24 AM
Replies To: Conversion string ->double
#3
Re: Conversion string ->double
Posted 10 September 2012 - 11:40 PM
You can try to parse it using the InvariantCulture, which will have the same effect across all systems:
string dblStr = "0.5"; double dbl = Double.Parse(dblStr, CultureInfo.InvariantCulture);
#4
Re: Conversion string ->double
Posted 12 September 2012 - 04:30 PM
double.Parse(string, IFormatProvider) is probably your best bet. Rule of thumb: Use doubles for >1.0 and floats for <1.0. For what I do, it works fantastic.
#5
Re: Conversion string ->double
Posted 16 September 2012 - 08:47 AM
You might use .ToString("0.00") to accomplish desired format
double myDbl = Convert.ToDouble(textBox5.Text);
textBlock32.Text = myDbl.ToString("0.00"); //you can also grab this and maybe need it later rounded up and displaying .ToString("0") to display whole again even if number is 787
double myDbl = Convert.ToDouble(textBox5.Text);
textBlock32.Text = myDbl.ToString("0.00"); //you can also grab this and maybe need it later rounded up and displaying .ToString("0") to display whole again even if number is 787
#6
Re: Conversion string ->double
Posted 16 September 2012 - 10:53 AM
#7
Re: Conversion string ->double
Posted 17 September 2012 - 08:49 AM
rangerofthewest, on 12 September 2012 - 06:30 PM, said:
Rule of thumb: Use doubles for >1.0 and floats for <1.0. For what I do, it works fantastic.
That's just pure nonsense. Doubles have higher resolutions than floats. It has nothing to do with values less or greater than 1. In fact, if you have values much smaller than 1 that needs a lot of precision, double would be a better choice. If you actually want to know what's going on with floats/doubles/decimals, read up:
Float
Appx. range: -3.4 × 1038 to +3.4 × 1038
Precision: 7 digits.
Size: 32 bits.
Double
Appx range: ±5.0 × 10−324 to ±1.7 × 10308
Precision: 15-16 digits
Size: 64 bits.
Decimal
Appx range: (-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
Precision: 28-29 significant digits
Size: 128 bits.
Note: Good for values that can't afford a loss of precision, but takes more memory and calculations against it are slower.
Also, please read the following two articles for the differences between binary floating point numbers (float, double), and decimal floating point numbers (decimal).
http://csharpindepth...atingPoint.aspx
http://csharpindepth...al/Decimal.aspx
#8
Re: Conversion string ->double
Posted 17 September 2012 - 08:57 AM
Hello guys, I though that I had resolved with that Culture invariant info, but no way.
Let start from the beginning.
I read data from US db, so I get, say 35.80. When I display this value in a textbox and I run the code in italian server, I see 35,80.
So maybe the problem is in this visualization, if i keep data in double, the appear to mantain the proper format, but if i display them, they get converted according to italian system.
What is the solution?
Let start from the beginning.
I read data from US db, so I get, say 35.80. When I display this value in a textbox and I run the code in italian server, I see 35,80.
So maybe the problem is in this visualization, if i keep data in double, the appear to mantain the proper format, but if i display them, they get converted according to italian system.
What is the solution?
#9
Re: Conversion string ->double
Posted 17 September 2012 - 09:15 AM
So your problem really is output, not input. Understand that the way this data is stored in memory is completely different than the way it is displayed. Things like commas and decimals are display issues, not value issues. So, if the number is correct in memory, you can always make it be displayed the way you want.
For instance, you might be able to specify the culture you want to use:
valStr should now be "0.5".
Or, if this is ASP.NET, you can set the culture info on the page itself, or on the master page (which you are hopefully using).
http://forums.asp.net/t/1095980.aspx/1
For instance, you might be able to specify the culture you want to use:
var val = .5;
var valStr = val.ToString(new CultureInfo("en-US"));
valStr should now be "0.5".
Or, if this is ASP.NET, you can set the culture info on the page itself, or on the master page (which you are hopefully using).
http://forums.asp.net/t/1095980.aspx/1
#10
Re: Conversion string ->double
Posted 01 October 2012 - 07:37 AM
I still cannot solve my problem
I have tried like you suggested but I had a compile error saying that the method ToString does not allow one argument.
So I tried like this.
Convert.ToString((cart[ii].pr_basket.price_value ,new CultureInfo("en-US"))
but problem persists. the value is 0.0 but is shown as 0 because all zeros after the dot are eliminated.
I have tried like you suggested but I had a compile error saying that the method ToString does not allow one argument.
So I tried like this.
Convert.ToString((cart[ii].pr_basket.price_value ,new CultureInfo("en-US"))
but problem persists. the value is 0.0 but is shown as 0 because all zeros after the dot are eliminated.
#11
Re: Conversion string ->double
Posted 01 October 2012 - 08:24 AM
You are using the wrong ToString() method, as well as doing it "The Hard Way".
Use the variant of ToString() that takes a formatting specifier and a culture/IFormatProvider specifier: http://msdn.microsof...y/18sthszb.aspx . What is even better is that you can call this directly on your decimal value rather than having to call the Convert class.
Pay attention to the second chunk of code where the sample code shows converting to a currency string.
Use the variant of ToString() that takes a formatting specifier and a culture/IFormatProvider specifier: http://msdn.microsof...y/18sthszb.aspx . What is even better is that you can call this directly on your decimal value rather than having to call the Convert class.
Pay attention to the second chunk of code where the sample code shows converting to a currency string.
This post has been edited by Skydiver: 01 October 2012 - 08:47 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|