10 Replies - 1114 Views - Last Post: 01 October 2012 - 08:24 AM Rate Topic: -----

#1 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

Conversion string ->double

Posted 10 September 2012 - 11:21 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Conversion string ->double

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Conversion string ->double

Posted 10 September 2012 - 11:39 PM

You can use double.Parse(string, IFormatProvider) where IFormatProvider is your culture

Examples of IFormatProviders are shown here
Was This Post Helpful? 1
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

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);

Was This Post Helpful? 1
  • +
  • -

#4 rangerofthewest  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 12
  • Joined: 10-September 12

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.
Was This Post Helpful? 0
  • +
  • -

#5 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

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
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Conversion string ->double

Posted 16 September 2012 - 10:53 AM

View Postrangerofthewest, on 13 September 2012 - 12:30 AM, said:

Rule of thumb: Use doubles for >1.0 and floats for <1.0. For what I do, it works fantastic.


Whaaat? I see no logic in doing that at all.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Conversion string ->double

Posted 17 September 2012 - 08:49 AM

View Postrangerofthewest, 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
Was This Post Helpful? 0
  • +
  • -

#8 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

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?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

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:

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
Was This Post Helpful? 0
  • +
  • -

#10 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

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.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

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.

This post has been edited by Skydiver: 01 October 2012 - 08:47 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1