Help: Convert string to decimal (or double) 
Author Message
 Help: Convert string to decimal (or double)

Greeetings
I have a string that represents a price and I need to convert it to a
decimal or a double.
The string is, e.g., string str="3.1415";
When I convert it to double I get 31415 istead of 3.1415

string str="3.1415";
double myDouble=Convert.ToDouble(str);
// myDouble has the value 31415.

I've also tried the System.Globalization.NumberFormatInfo but I don't
seem to get it work :(
Any help is needed ;)

Thanks

--
-------------------------------------------------------------------------
Srgio Almeida
MobiComp - Mobile Computing & Wireless Solutions
phone: +351 253 305 250     fax: +351 253 305 251
web: http://www.*-*-*.com/
-------------------------------------------------------------------------



Tue, 31 May 2005 20:00:19 GMT  
 Help: Convert string to decimal (or double)


Wed, 18 Jun 1902 08:00:00 GMT  
 Help: Convert string to decimal (or double)
String.Format ("{0:0.00}", 0.12d);

Read up on fhe format strings.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)


Quote:
> Greeetings
> I have a string that represents a price and I need to convert it to a
> decimal or a double.
> The string is, e.g., string str="3.1415";
> When I convert it to double I get 31415 istead of 3.1415

> string str="3.1415";
> double myDouble=Convert.ToDouble(str);
> // myDouble has the value 31415.

> I've also tried the System.Globalization.NumberFormatInfo but I don't
> seem to get it work :(
> Any help is needed ;)

> Thanks

> --
> -------------------------------------------------------------------------
> Srgio Almeida
> MobiComp - Mobile Computing & Wireless Solutions
> phone: +351 253 305 250     fax: +351 253 305 251
> web: http://www.mobicomp.com
> -------------------------------------------------------------------------



Tue, 31 May 2005 20:35:39 GMT  
 Help: Convert string to decimal (or double)
You might want to look at Double.Parse or Double.TryParse.

--Bob


Quote:
> Greeetings
> I have a string that represents a price and I need to convert it to a
> decimal or a double.
> The string is, e.g., string str="3.1415";
> When I convert it to double I get 31415 istead of 3.1415

> string str="3.1415";
> double myDouble=Convert.ToDouble(str);
> // myDouble has the value 31415.

> I've also tried the System.Globalization.NumberFormatInfo but I don't
> seem to get it work :(
> Any help is needed ;)

> Thanks

> --
> -------------------------------------------------------------------------
> Srgio Almeida
> MobiComp - Mobile Computing & Wireless Solutions
> phone: +351 253 305 250     fax: +351 253 305 251
> web: http://www.mobicomp.com
> -------------------------------------------------------------------------



Tue, 31 May 2005 21:52:47 GMT  
 Help: Convert string to decimal (or double)
     CultureInfo ci = new CultureInfo("en-US");
     myDouble = double.Parse(str, NumberStyles.Currency, ci);

Does your culture use . as the thousands separator. another words do you
print 1000 as 1.000,00?

this would explain why the parse returned 31415 because the parse function
is culture aware and follows the rule of the current thread culture.

Joe Feser


Quote:
> Greeetings
> I have a string that represents a price and I need to convert it to a
> decimal or a double.
> The string is, e.g., string str="3.1415";
> When I convert it to double I get 31415 istead of 3.1415

> string str="3.1415";
> double myDouble=Convert.ToDouble(str);
> // myDouble has the value 31415.

> I've also tried the System.Globalization.NumberFormatInfo but I don't
> seem to get it work :(
> Any help is needed ;)

> Thanks

> --
> -------------------------------------------------------------------------
> Srgio Almeida
> MobiComp - Mobile Computing & Wireless Solutions
> phone: +351 253 305 250     fax: +351 253 305 251
> web: http://www.mobicomp.com
> -------------------------------------------------------------------------



Tue, 31 May 2005 22:58:48 GMT  
 Help: Convert string to decimal (or double)
.NET used the current Windows regional settings (Control Panel).

If the string is always with a dot '.' as decimal separator,
try the 'InvariantCulture' :

   double d = Double.Parse( "3.1415", CultureInfo.InvariantCulture );

or switch thread culture:

   Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
   dd = Double.Parse( "3.1415" );

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusinginvari...

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconformattingn...

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconnumericform...

--
 Thomas Scheidegger - MVP .NET - 'NETMaster'
 http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/



Wed, 01 Jun 2005 00:32:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How do I convert DOS decimal to MVS zoned decimal

2. Convert from binary to decimal, hex to decimal etc

3. Converting from double to currency string

4. about converting double to string

5. Convert a String to float/double

6. Convert a string to a double value ?

7. Help:convert binary to decimal numbers

8. Convert String to Double

9. Converting double to string

10. IEEE-754 code to convert to and fro double/hex string

11. Converting a string containing hex to a double

12. Converting from a double to a string

 

 
Powered by phpBB® Forum Software