How to convert this scientific notation to decimal? - c#

After search in google, using below code still can not be compiled:
decimal h = Convert.ToDecimal("2.09550901805872E-05");
decimal h2 = Decimal.Parse(
"2.09550901805872E-05",
System.Globalization.NumberStyles.AllowExponent);

You have to add NumberStyles.AllowDecimalPoint too:
Decimal.Parse("2.09550901805872E-05", NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
MSDN is clear about that:
Indicates that the numeric string can be in exponential notation. The
AllowExponent flag allows the parsed string to contain an exponent
that begins with the "E" or "e" character and that is followed by an
optional positive or negative sign and an integer. In other words, it
successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx.
It does not allow a decimal separator or sign in the significand or
mantissa; to allow these elements in the string to be parsed, use the
AllowDecimalPoint and AllowLeadingSign flags, or use a composite style
that includes these individual flags.

Since decimal separator ("." in your string) can vary from culture to culture
it's safier to use InvariantCulture. Do not forget to allow this decimal
separator (NumberStyles.Float)
decimal h = Decimal.Parse(
"2.09550901805872E-05",
NumberStyles.Float | NumberStyles.AllowExponent,
CultureInfo.InvariantCulture);
Perharps, more convenient code is when we use NumberStyles.Any:
decimal h = Decimal.Parse(
"2.09550901805872E-05",
NumberStyles.Any,
CultureInfo.InvariantCulture);

use System.Globalization.NumberStyles.Any
decimal h2 = Decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any);

decimal h = Convert.ToDecimal("2.09550901805872E-05");
decimal h2 = decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any)

This thread was very helpful to me. For the benefit of others, here is complete code:
var scientificNotationText = someSourceFile;
// FileTimes are based solely on nanoseconds.
long fileTime = 0;
long.TryParse(scientificNotationText, NumberStyles.Any, CultureInfo.InvariantCulture,
out fileTime);
var dateModified = DateTime.FromFileTime(fileTime);

Decimal h2 = 0;
Decimal.TryParse("2.005E01", out h2);

Related

Format decimal number with custom amount of digits after comma

I have this custom extension that should format a decimal number with a custom amount of digits after comma.
public static decimal FormatDecimal(this decimal value, int decimalSeparator = 2)
{
decimal returnValue = Math.Round(value, decimalSeparator, MidpointRounding.AwayFromZero);
return returnValue;
}
The problem is that doesn't work as expected.
If I do like this:
decimal number = 12345;
and then:
decimal formatedNumber = number.FormatDecimal(2);
the result should be:
12345.00
instead the result is:
12345
What I am doing wrong?
Here's the extension function working
public static string FormatDecimal(this decimal value, int decimalSeparator = 2)
{
return value.ToString(string.Format("0.{0}", new string('0', decimalSeparator)));
}
I think the right way is to using The "0" custom format specifier;
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
For example;
decimal d = 12345;
Console.WriteLine(d.ToString("#.00")); // 12345.00
You're probably looking to format the string representation of your decimal instead. Try this:
decimal myNumber = 12345.67m;
string formattedNumber = myNumber.ToString("N3");
Console.WriteLine(formattedNumber); // Prints "12345.670"
See here for more information: https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx
From MSDN:
Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form Axx, where:
A is a single alphabetic character called the format specifier. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For more information, see Custom Numeric Format Strings.
xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Note that the precision specifier controls the number of digits in the string representation of a number. It does not round the number itself. To perform a rounding operation, use the Math.Ceiling, Math.Floor, or Math.Round method.
When precision specifier controls the number of fractional digits in the result string, the result strings reflect numbers that are rounded away from zero (that is, using MidpointRounding.AwayFromZero).
You should specify the formatting when you display the string.
What you need is to do the following, when you convert to string:
String.Format("{0:0.00}", formatedNumber);
Refer to This article for more details:

Double to string with mandatory decimal point

This is probably dumb but it's giving me a hard time. I need to convert/format a double to string with a mandatory decimal point.
1 => 1.0
0.2423423 => 0.2423423
0.1 => 0.1
1234 => 1234.0
Basically, I want to output all decimals but also make sure the rounded values have the redundant .0 too. I am sure there is a simple way to achieve this.
Use double.ToString("N1"):
double d1 = 1d;
double d2 = 0.2423423d;
double d3 = 0.1d;
double d4 = 1234d;
Console.WriteLine(d1.ToString("N1"));
Console.WriteLine(d2.ToString("N1"));
Console.WriteLine(d3.ToString("N1"));
Console.WriteLine(d4.ToString("N1"));
Demo
Standard Numeric Format Strings
The Numeric ("N") Format Specifier
Update
(1.234).ToString("N1") produces 1.2 and in addition to removing additional decimal digits, it also adds a thousands separator
Well, perhaps you need to implement a custom NumberFormatInfo object which you can derive from the current CultureInfo and use in double.ToString:
var culture = CultureInfo.CurrentCulture;
var customNfi = (NumberFormatInfo)culture.NumberFormat.Clone();
customNfi.NumberDecimalDigits = 1;
customNfi.NumberGroupSeparator = "";
Console.WriteLine(d1.ToString(customNfi));
Note that you need to clone it since it's readonly by default.
Demo
There is not a built in method to append a mandatory .0 to the end of whole numbers with the .ToString() method, as the existing formats will truncate or round based on the number of decimal places you specify.
My suggestion is to just roll your own implementation with an extension method
public static String ToDecmialString(this double source)
{
if ((source % 1) == 0)
return source.ToString("f1");
else
return source.ToString();
}
And the usage:
double d1 = 1;
double d2 = 0.2423423;
double d3 = 0.1;
double d4 = 1234;
Console.WriteLine(d1.ToDecimalString());
Console.WriteLine(d2.ToDecimalString());
Console.WriteLine(d3.ToDecimalString());
Console.WriteLine(d4.ToDecimalString());
Results in this output:
1.0
0.2423423
0.1
1234.0
You could do something like this: if the number doesn't have decimal points you can format its output to enforce one decimal 0 and if it has decimal places, just use ToString();
double a1 = 1;
double a2 = 0.2423423;
string result = string.Empty;
if(a1 - Math.Floor(a1) >0.0)
result = a1.ToString();
else
result = a1.ToString("F1");
if (a2 - Math.Floor(a2) > 0.0)
result = a2.ToString();
else
result = a2.ToString("F1");
When you use "F" as formatting, the output won't contain thousands separator and the number that follows it specifies the number of decimal places.
Use ToString("0.0###########################").
It does work. I found it in duplicate of your question decimal ToString formatting which gives at least 1 digit, no upper limit
Double provides a method ToString() where you can pass an IFormatProvider-object stating how you want your double to be converted.
Additionally, it should display trailing 0 at all costs.
value = 16034.125E21;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
// -16325.62015
// -16325.62015
// -16325,62015
// 1.6034125E+25
// 1.6034125E+25
// 1,6034125E+25
Here is the documentation from MSDN.
You can cast to string and then appen ".0" if there was no decimal point given
string sValue=doubleValue.ToString();
if(!sValue.Contains('.'))
sValue+=".0";
EDIT:
As mentioned in the comments '.' may not be the decimal seperator in the current culture. Refer to this article to retrieve the actual seperator if you want make your code save for this case.

How to parse string to decimal with currency symbol?

I have no idea why this is not working:
string s = "12,00 €";
var germanCulture = CultureInfo.CreateSpecificCulture("de-DE");
decimal d;
if (decimal.TryParse(s, NumberStyles.AllowCurrencySymbol, germanCulture, out d))
{
// i want to get to this point
Console.WriteLine("Decimal value: {0}", d);
}
Use NumberStyles.Currency instead of NumberStyles.AllowCurrencySymbol
if (decimal.TryParse(s, NumberStyles.Currency, germanCulture, out d))
and the output for you code would be:
Decimal value: 12.00
Try this;
string.Format(new CultureInfo("en-SG", false), "{0:c0}", 123423.083234);
It will convert 123423.083234 to $1,23,423 format.
Please note that this doesn't work with the bitcoin symbol and other cryptocurrency symbols yet, unfortunately. I tried both ways.
Sharplab proof: link
A workaround in my case (I have a minus sign followed by the bitcoin ฿ symbol) would be to do
a .Where() on string ignoring everything except numbers and minus sign... But if you want general validation... Well then you'll have to hardcode these bitcoin etc symbols somewhere

Parse a Number from Exponential Notation

I need to parse the string "1.2345E-02" (a number expressed in exponential notation) to a decimal data type, but Decimal.Parse("1.2345E-02") simply throws an error
It is a floating point number, you have to tell it that:
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
It works if you specify NumberStyles.Float:
decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float);
Console.WriteLine(x); // Prints 0.012345
I'm not entirely sure why this isn't supported by default - the default is to use NumberStyles.Number, which uses the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles. Possibly it's performance-related; specifying an exponent is relatively rare, I suppose.
In addition to specifying the NumberStyles I would recommend that you use the decimal.TryParse function such as:
decimal result;
if( !decimal.TryParse("1.2345E-02", NumberStyles.Any, CultureInfo.InvariantCulture, out result) )
{
// do something in case it fails?
}
As an alternative to NumberStyles.Any you could use a specific set if you're certain of your formats. e.g:
NumberStyles.AllowExponent | NumberStyles.Float
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
Be cautious about the selected answer: there is a subtility specifying System.Globalization.NumberStyles.Float in Decimal.Parse which could lead to a System.FormatException because your system might be awaiting a number formated with ',' instead of '.'
For instance, in french notation, "1.2345E-02" is invalid, you have to convert it to "1,2345E-02" first.
In conclusion, use something along the lines of:
Decimal.Parse(valueString.Replace('.',','), System.Globalization.NumberStyles.Float);
The default NumberStyle for decimal.Parse(String) is NumberStyles.Number, so if you just want to add the functionality to allow exponents, then you can do a bitwise OR to include NumberStyles.AllowExponent.
decimal d = decimal
.Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);
I've found that passing in NumberStyles.Float, in some cases, changes the rules by which the string is processed and results in a different output from NumberStyles.Number (the default rules used by decimal.Parse).
For example, the following code will generate a FormatException in my machine:
CultureInfo culture = new CultureInfo("");
culture.NumberFormat.NumberDecimalDigits = 2;
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.NumberGroupSeparator = ",";
Decimal.Parse("1,234.5", NumberStyles.Float, culture); // FormatException thrown here
I'd recommend using the input NumberStyles.Number | NumberStyles.AllowExponent, as this will allow exponential numbers and will still process the string under the decimal rules.
CultureInfo culture = new CultureInfo("");
culture.NumberFormat.NumberDecimalDigits = 2;
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.NumberGroupSeparator = ",";
Decimal.Parse("1,234.5",NumberStyles.Number | NumberStyles.AllowExponent, culture); // Does not generate a FormatException
To answer the poster's question, the right answer should instead be:
decimal x = decimal.Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);
Console.WriteLine(x);
Warning about using NumberStyles.Any:
"6.33E+03" converts to 6330 as expected. In German, decimal points are represented by commas, but 6,33E+03 converts to 633000! This is a problem for my customers, as the culture that generates the data is not known and may be different than the culture that is operating on the data. In my case, I always have scientific notation, so I can always replace comma to decimal point before parsing, but if you are working with arbitrary numbers, like pretty-formatted numbers like 1,234,567 then that approach doesn't work.
You don't need to replace the dots (respectively the commas) just specify the input IFormatProvider:
float d = Single.Parse("1.27315", System.Globalization.NumberStyles.Float, new CultureInfo("en-US"));
float d = Single.Parse("1,27315", System.Globalization.NumberStyles.Float, new CultureInfo("de-DE"));
If you want to check and convert the exponent value use this
string val = "1.2345E-02";
double dummy;
bool hasExponential = (val.Contains("E") || val.Contains("e")) && double.TryParse(val, out dummy);
if (hasExponential)
{
decimal d = decimal.Parse(val, NumberStyles.Float);
}
Hope this helps someone.

Double.TryParse() input decimal separator different than system decimal separator

I have a source XML that uses a dot (".") as a decimal separator and I am parsing this on a system that uses a comma (",") as a decimal separator.
As a result, value of 0.7 gets parsed with Double.TryParse or Double.Parse as 7000000.
What are my options to parse correctly? One of them is to replace dots in source with commas with String.Replace('.', ',') but I don't think I like this very much.
XML standard is explicit about the formatting of dates and numbers etc. This helps to ensure that the XML is platform independent and interoperable. Take a look at using XmlConvert for xml data.
double value = XmlConvert.ToDouble(stringValue);
This does the job:
string test = "0.7";
Assert.Equal(0.7, Double.Parse(test, NumberStyles.Float, CultureInfo.InvariantCulture));
double.TryParsehas an overload taking an IFormatProvider. Use a coresponding CultureInfo, in your case CultureInfo.InvariantCulture can be used.
Easy way to specify custom decimal separator:
var price = "122$00";
var nfi = new NumberFormatInfo { CurrencyDecimalSeparator = "$" };
var ok = decimal.TryParse(price, NumberStyles.Currency, nfi, out result);

Categories