Parse strings to double with comma and point
You want to treat dot (.
) like comma (,
). So, replace
if (double.TryParse(values[i, j], out tmp))
with
if (double.TryParse(values[i, j].Replace('.', ','), out tmp))
How do I parse string with decimal separator to a double c#?
In addition to replace comma with dot, you need to supply a proper number format:
public double ParseMyString(string myString)
{
return double.Parse(myString.Replace(',', '.'),
new NumberFormatInfo() {NumberDecimalSeparator = "."});
}
Another option to replace the separator on a broader scope is to use this:
Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
You will still need to replace comma with dot though.
C# Convert string to double/decimal and back to string, keeping trailing zeroes, adding comas for thousands
double.Parse(input)
is a no go, as double
does not keep track of the number of decimals.
decimal.Parse(input).ToString()
will show that decimal
does keep track of that. Unfortunately, decimal.Parse(input).ToString()
uses this precision and doesn't use a thousands separator, and decimal.Parse(input).ToString("N")
ignores the precision but does use a thousands separator.
Manually extracting the precision from the decimal works though, and that allows you to build the correct format string:
static string InsertThousandsSeparator(string input) {
var dec = decimal.Parse(input);
var bits = decimal.GetBits(dec);
var prec = bits[3] >> 16 & 255;
return dec.ToString("N" + prec);
}
This is based on the layout of decimal
as described on MSDN:
Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.
See it working on .NET Fiddle. (courtesy of @Alisson)
How to parse String, with both decimal separators comma and dot as well, to Double
Like Mohit Thakur said, but compilable.
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
try {
Number number = format.parse(formDto.getWeight().replace('.', ','));
kitten.setWeight(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}
How to parse String to double with 18 decimal values in Dart/Flutter?
Precise(
String value,
{int sigDigits = 50}
)
Constructs an arbitrary precision number from a string.
The precision can be limited by providing the maximum number of significant digits (default is 50).
Examples: Precise('12') Precise('0.1234') Precise('15.123456789101112131') Precise('1.23456789e-6', sigDigits: 4)
Precise constructor
Convert string to double with 2 digit after decimal separator
Doubles can't exactly represent 16.9. I suggest you convert it to decimal
instead:
string s = "16.9";
decimal m = Decimal.Parse(s) * 100;
double d = (double)m;
You might just want to keep using the decimal
instead of the double
, since you say you'll be using it for monetary purposes. Remember that decimal
is intended to exactly represent decimal numbers that fit in its precision, while double
will only exactly represent binary numbers that do.
Related Topics
Parameterized Query For MySQL With C#
What's the @ in Front of a String in C#
Virtual Member Call in a Constructor
Listing All Permutations of a String/Integer
In C#, Difference Between Public, Private, Protected, and Having No Access Modifier
Do Httpclient and Httpclienthandler Have to Be Disposed Between Requests
The Located Assembly'S Manifest Definition Does Not Match the Assembly Reference
Await' Works, But Calling Task.Result Hangs/Deadlocks
Processstartinfo Hanging on "Waitforexit" - Why
Combining Two Expressions (Expression≪Func≪T, Bool≫≫)
What Is a Good Pattern For Using a Global Mutex in C#
Watermark/Hint/Placeholder Text in Textbox
Filtering on Include in Ef Core
How to Make a Textbox That Only Accepts Numbers
Best Way to Randomize an Array With .Net