Multimobile Development: Building Applications for any Smartphone
Decimals and scientific notation?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.


Jon Schwartz
GOOD ANSWER
I'd like to convert or parse string values in scientific
notation into a decimal. Since I can assign numberic
values to decimals in this format, I thought strings
would also convert, but I get an error from the
Formatter.

This works:
decimal frequency = 3E6M;

This doesn't:
frequency = Convert.ToDecimal("3E6");

I also tried:
frequency = Convert.ToDecimal("3E6M");

Is this something I'm going to need to build my own
format provider for? Does anyone have a template
provider you could post? Not necessarily this one; I can
copy and modify any format provider. Starting with an
example that works would be a big help.

Thanks in advance!
Jon Schwartz
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Jon Schwartz
GOOD ANSWER
Answering my own question, in case anyone needs the
info. The Convert object does not support the use of
NumberStyles (such as Scientific Notation), but the Parse
function (called by Convert) does support the use of
number styles. In order to parse a number in scientific
notation into a decimal variable (such as 3E6):

Decimal.Parse(strExpression,
System.Globalization.NumberStyles.AllowExponent));

Cheers,
Jon
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Magnus Werner
GOOD ANSWER
Well

Decimal d = Decimal.Parse("6e3", System.Globalization.NumberStyles.AllowExponent);

works but

Decimal d = Decimal.Parse("5.39799999999999e3", System.Globalization.NumberStyles.AllowExponent); or
Decimal d = Decimal.Parse("5.39799999999999e3M", System.Globalization.NumberStyles.AllowExponent);

does not!!! And this is what I need! You are allowed to write

Decimal f = 5.39799999999999e3M;

in the code however. Any suggestions greatly appreciated. I'm using the .NET 1.1 Framework by the way.

Regards Magnus
--------------------------------
From: Magnus Werner
Reply to this message...
Vote that this is a GOOD answer... (7 votes from other users already)
 
 
    
Jian-Wei Yu (MS)
GOOD ANSWER
Hello,

Thank you for using the Microsoft VB Newsgroups.

Have you tried the Parse method of the Double or Single structure?

Hope it helps. If you have any questions, please reply to this post.

Regards,

Jian-Wei Yu
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Benjamin J. J. Voigt
GOOD ANSWER
Hi,

You need to use the formater System.Globalization.NumberStyles.Any as in:
Decimal d = Decimal.Parse("-9.9876523372918328345612998256E-05", System.Globalization.NumberStyles.Any );
Console.Out.WriteLine(d.ToString());

Regards Ben

P.S: Only 28 digits precision BTW, http://www.yoda.arachsys.com/csharp/decimal.html

--------------------------------
From: Benjamin J. J. Voigt
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Benjamin J. J. Voigt
GOOD ANSWER
Hi,

You need to use the formater System.Globalization.NumberStyles.Any as in:
Decimal d = Decimal.Parse("-9.9876523372918328345612998256E-05", System.Globalization.NumberStyles.Any );
Console.Out.WriteLine(d.ToString());

Regards Ben

P.S: Only 28 digits precision BTW, http://www.yoda.arachsys.com/csharp/decimal.html

--------------------------------
From: Benjamin J. J. Voigt
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Benjamin J. J. Voigt
GOOD ANSWER
Hi,

You need to use the formater System.Globalization.NumberStyles.Any as in:
Decimal d = Decimal.Parse("-9.9876523372918328345612998256E-05", System.Globalization.NumberStyles.Any );
Console.Out.WriteLine(d.ToString());

Regards Ben

P.S: Only 28 digits precision BTW, http://www.yoda.arachsys.com/csharp/decimal.html

--------------------------------
From: Benjamin J. J. Voigt
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Benjamin J. J. Voigt
GOOD ANSWER
Hi,

You need to use the formater System.Globalization.NumberStyles.Any as in:
Decimal d = Decimal.Parse("-9.9876523372918328345612998256E-05", System.Globalization.NumberStyles.Any );
Console.Out.WriteLine(d.ToString());

Regards Ben

P.S: Only 28 digits precision BTW, http://www.yoda.arachsys.com/csharp/decimal.html

--------------------------------
From: Benjamin J. J. Voigt
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Benjamin J. J. Voigt
GOOD ANSWER
Hi,

You need to use the formater System.Globalization.NumberStyles.Any as in:
Decimal d = Decimal.Parse("-9.9876523372918328345612998256E-05", System.Globalization.NumberStyles.Any );
Console.Out.WriteLine(d.ToString());

Regards Ben

P.S: Only 28 digits precision BTW, http://www.yoda.arachsys.com/csharp/decimal.html

--------------------------------
From: Benjamin J. J. Voigt
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
 
System.Console
System.Convert
System.Decimal
System.Globalization.NumberStyles




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734