Problem in converting string into double in C# [duplicate] - c#

This question already has answers here:
String to decimal conversion: dot separation instead of comma
(8 answers)
Closed 6 months ago.
I need a program that takes two inputs from the user and perform a calculation. Those inputs need to be double datatype.
The problem I'm facing is that: the program receives a decimal value with dot separator, but when converting it to double, it loses the decimal value. For example, input from the user is 2.5, but when converting it becomes 25.
When the user type 2,5 it is correctly converting it to 2.5
Here's my code example:
Console.WriteLine("Var1: ");
string? v1 = Console.ReadLine();
Console.WriteLine("Var2: ");
string? v2 = Console.ReadLine();
double v1Double = Double.Parse(v1);
double v2Double = Double.Parse(v2);
Console.WriteLine($"Var1: {v1Double}");
Console.WriteLine($"Var2: {v2Double}");
Console.WriteLine($"Multiplication: {v1Double * v2Double}");
Here's what I'm getting when with dot separator:
Here's what I'm gettig when comma separator:
Can anyone help me how to address this problem?

You could use NumberFormatInfo for this problem like so:
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberDecimalSeparator = ".";
provider.NumberGroupSeparator = ",";
double doubleVal = Convert.ToDouble(YOUR_STRING, provider);

Related

Formatting a decimal as currency with plus and minus signs in c# [duplicate]

This question already has answers here:
How to use NumberFormatInfo to remove parenthesis for negative values
(6 answers)
Closed 5 years ago.
Is there a way to format a decimal so that it appears as a currency with both + or - signs?
For example:
+$5.00 (plus sign for greater than zero)
$0.00 (no sign for zero)
-$5.00 (minus sign for less than zero)
The following does what I want but not sure how to incorporate currency:
var formattedprice = $"{price:+0;-#}"
I would typically use C0 for currency or N0 for number.
class Program
{
static void Main(string[] args)
{
var pos = 5m;
var zero = 0m;
var neg = -5m;
var format = "+$0.00;-$0.00;$0.00";
Console.WriteLine(pos.ToString(format));
Console.WriteLine(zero.ToString(format));
Console.WriteLine(neg.ToString(format));
}
}
and the output is
+$5,00
$0,00
-$5,00
and as per #xxbbcc's comment, currency symbol location depends on locale so you'll have to change the format in accordance.

how to convert from double to exponential notation in c# [duplicate]

This question already has an answer here:
C# how to convert a double to string with exponential notation
(1 answer)
Closed 6 years ago.
How to convert double number to exponential notation in c#?
My number
I would like number will look like:
-1.6500000000000000e1
I looks on the article:
C# how to convert a double to string with exponential notation
But this didn't fully answer me:
number.ToString("e16", CultureInfo.InvariantCulture)
provide me number looks like:
-1.6500000000000000e+001
I would like at the end only e1 for non negative, or e-1 for negative
Thanks!
Try using formatting:
Double value = -1.6500000000000000e1;
// e15 - exponential form, small "e" for exponent, 16 digits
String result = value.ToString("e16", CultureInfo.InvariantCulture);
Console.Write(result);
Edit: in case you want "e1" form of exponent, you should specify it like this:
String result = value.ToString("0.0000000000000000e0", CultureInfo.InvariantCulture);

How to hide fractional part if it is zero? [duplicate]

This question already has answers here:
How do I format a double to a string and only show decimal digits when necessary?
(2 answers)
Closed 9 years ago.
How to hide fractional part of a double or float number if it is all zero. I am converting a floating point number to string and its display Mantissa part even if it is zero. For example:
double number = 123.00;
string strNumber = number.ToString(); // it shows "123.0", what I need is only "123"
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString(); // it shows "123.2234" as needed.
Is there any built-in solution in .NET to get it done?
Thanks
Try using the overload of double.ToString() that takes in a format string, and pass it "R":
double number = 123.00;
string strNumber = number.ToString("R");
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");

Set 0 at Decimal place if no value found [duplicate]

This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 9 years ago.
I need to set “0” at the end of the decimal place dynamically if less integer number found after decimal place.
Suppose we have value: “535.8”
Now I need to set it as “535.800”
I have following code:
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
Console.WriteLine(price);
Console.ReadLine();
Unable to get 535.800.
How can I achieve this?
You can convert price to string and show upto 3 decimal places with 0's at end.
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
//string.Format("{0:N2}", price);
Console.WriteLine(string.Format("{0:N3}", price));
price.ToString("N3")
Standard Numeric Format Strings: The Numeric ("N") Format Specifier
So if the number of decimal should be dynamic:
int numDecimalPlaces = 3;
Console.WriteLine(price.ToString("N" + numDecimalPlaces));
You can use string.Format() to make it possible:
Console.WriteLine(string.Format("{0:N3}", d));
So in your code:
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
Console.WriteLine(string.Format("{0:N" + decplace + "}", price);
Console.ReadLine();

Convert String into Double - Result is failing [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reading a double value from a string
I have a problem with converting my String into a double, I always get strange results.
I want to convert the following string:
string Test = "17828.571428571";
I tried it like this (because it normally works):
Double _Test = Convert.ToDouble(Test);
Result is: 17828571428571 (without the dot, lol)
I need it as a double, to Math.Round() afterwards, so I have 17828 in my example.
My second idea was to split the string, but is that really the best method? :S
Thanks for helping me!
Finn
Use InvariantCulture
Double _Test = Convert.ToDouble(Test,CultureInfo.InvariantCulture);
EDIT: I believe your current culture is "German" de-DE, which uses , for floating point.
Tried the following code. You may also use NumberFormatInfo.InvariantInfo during conversion.
string Test = "17828.571428571";
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
double d = Convert.ToDouble(Test);
double d2 = Convert.ToDouble(Test, CultureInfo.InvariantCulture);
double d3 = Convert.ToDouble(Test, NumberFormatInfo.InvariantInfo);
string Test2 = "17828,571428571"; //Notice the comma in the string
double d4 = Convert.ToDouble(Test2);
Console.WriteLine("Using comma as decimal point: "+ d4);
Output: (Notice the comma in the output)
Wihtout Invariant: 17828571428571
With InvariantCulture: 17828,571428571
With NumberFormatInfo.InvariantInfo: 17828,571428571
Using comma as decimal point: 17828,571428571
double _Test = double.Parse(Test,CultureInfo.InvariantCulture);
You need to set the format provider of the conversion operation to invariant.
Try Double.TryParse or Double.Parse if you are sure there is the correct format
EDIT: But take care of the format. as example if you are german you need to type 140,50 and not 140.50 because 140.50 would be 14050.
Or you pass as parameter that you dont care of culture (see other posts).

Categories