How to Parse a String With a Decimal Point to a Double

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



Leave a reply



Submit