|
| 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
|
|
|
| |
|
|
| |
| |
| 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
|
|
|
| |
|
|
| |
| |
| 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
|
|
|
| |
|
|
| |
|
|
| |
| 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.
|
|
|
| |
|
|
| |
|
| | |
| | |
| | |
| | |
| | |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|