double.parse convert two zero decimal to one decimal - c#

I have a string 10.00 and I want to convert it to double 10.00.
I use :
string str = "10.00";
double db = double.Parse(str);
the result I get is 10.0 and not 10.00.

The Parse and TryParse on the numeric respect local culture settings; you can change this by specifying a CultureInfo object. For instance, parsing 2.999 into a double gives 2999 in Germany:
Console.WriteLine (double.Parse ("2.999")); // 2999 (In Germany)
This is because in Germany, the period indicates a thousands separator rather than a decimal point. Specifying invariant culture fixes this:
double x = double.Parse ("2.999", CultureInfo.InvariantCulture);
The same when calling ToString():
string x = 2.9999.ToString (CultureInfo.InvariantCulture);

A double isn't a string. If you want to display the double as a string, you can format it to have two decimal points.
For example:
string str = "10.00";
double db = double.Parse(str);
String.Format("{0:0.00}", db); // will show 10.00

Question isn't really clear, but if you are referring to changing the double back to string with 2 decimal place precision, you can use:
string str = "10.00"
double db = double.parse(str);
string convertedBack = db.ToString("0.00");

Related

How to always show 3 numbers after decimal point C#

I am currently displaying a number that is being rounded to 3 decimal places e.g. 0.31, using Math.Pow, the only problem is I want to display this number to say 0.310 (for styling purposes) does anyone know if this is possible?
The Fixed-Point Format Specifier can be used in a call to ToString or in string.Format:
double x = 1493.1987;
string s1 = x.ToString("F3");
string s2 = string.Format("Your total is {0:F3}, have a nice day.", x);
// s1 is "1493.199"
// s2 is "Your total is 1493.199, have a nice day."
Note that the Fixed-Point Format Specifier will always show the number of decimal digits you specify. For example:
double y = 1493;
string s3 = y.ToString("F3");
// s3 is "1493.000"
Use the format in the toString
double pi = 3.1415927;
string output = pi.ToString("#.000");
Here is an updated example that also works w/o having to call .ToString():
float a = 12.3578f;
double b = 12.3578d;
Console.WriteLine("The tolerance specs are: {0:F4} and: {1:F3}", a,b);
ANSWER: The tolerance specs are: 12.3578 and: 12.358

How do I parse string with decimal separator to a double c#?

How to get correct double digit from string.
string first = "23.3";
string second = "23,3";
For now I used the sample parser for parse the number in double format:
double number = double.Parse(first);
double another = double.Parse(second);
So if I used en-US culture and for decimal separator used '.' then the result will be number = 23.3 and another = 233.
So my question is do is possible to ignore the decimal separator and when is parse in both case to return result = 23.3.
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.
You can play a trick:
private string ReplaceSeparator(string Num)
{
return Num.Replace(",", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
}
.....
string first = "23.3";
string second = "23,3";
first = ReplaceSeparator(first);
second = ReplaceSeparator(second);
double number = double.Parse(first);
double another = double.Parse(second);

How to convert string to decimal with 3 decimal places?

string num = 23.6;
I want to know how can I convert it into decimal with 3 decimal places
like
decimal nn = 23.600
Is there any method?
I try my best..
First of all your string num = 23.6; won't even compile. You need to use double quotes with your strings like string num = "23.6";
If you wanna get this as a decimal, you need to parse it first with a IFormatProvider that have . as a NumberDecimalSeparator like InvariantCulture(if your CurrentCulture uses . already, you don't have to pass second paramter);
decimal nn = decimal.Parse(num, CultureInfo.InvariantCulture);
Now we have a 23.6 as a decimal value. But as a value, 23.6, 23.60, 23.600 and 23.60000000000 are totally same, right? No matter which one you parse it to decimal, you will get the same value as a 23.6M in debugger. Looks like these are not true. See Jon Skeet comments on this answer and his "Keeping zeroes" section on Decimal floating point in .NET article.
Now what? Yes, we need to get it's textual representation as 23.600. Since we need only decimal separator in a textual representation, The "F" Format Specifier will fits out needs.
string str = nn.ToString("F3", CultureInfo.InvariantCulture); // 23.600
There are two different concepts here.
Value
View
you can have a value of 1 and view it like 1.0 or 1.0000 or +000001.00.
you have string 23.6. you can convert it to decimal using var d = decimal.Parse("23.6")
now you have a value equals to 23.6 you can view it like 23.600 by using d.ToString("F3")
you can read more about formatting decimal values
the thing that works for me in my case is num.ToString("#######.###")
A decimal is not a string, it does not display the trailing zeros. If you want a string that displays your 3 decimal places including trailing zeros, you can use string.Format:
decimal nn= 23.5;
var formattedNumber = string.Format("{0,000}", nn);

Convert decimal currency to comma separated value

I get from a webservice the following strings:
12.95
or
1,200.99
Is there an option to convert these values to the following values without manipulating the string?
12,95
or
1200,99
I tried it with some Culture options but didn't get it right...
EDIT
I tried this:
//return string.Format( "{0:f2}", Convert.ToDecimal( price ) );
//return string.Format(CultureInfo.GetCultureInfo("de-de"), "{0:0}", price);
NumberFormatInfo format = new System.Globalization.NumberFormatInfo();
format.CurrencyDecimalDigits = 2;
format.CurrencyDecimalSeparator = ",";
format.CurrencyGroupSeparator = "";
return decimal.Parse(price).ToString(format);
var input = "1,200.99";
//Convert to decimal using US culture (or other culture using . as decimal separator)
decimal value = decimal.Parse(input, CultureInfo.GetCultureInfo("en-US"));
//Convert to string using DE culture (or other culture using , as decimal separator)
string output = value.ToString(CultureInfo.GetCultureInfo("de-DE"));
Console.WriteLine(output); //1200,99
What about something like this:
double number;
double.TryParse("1,200.99", NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-US"), out number);
var formattedNumber = number.ToString(CultureInfo.CreateSpecificCulture("de-DE"));
Then return or write out formattedNumber (whatever you need to do).
Yes and no. First, what you have is a string, and so you cannot change the formatting of it as you're attempting to. However, to achieve what you would like, you can parse the string into a decimal value and then use the formatting options for decimals to display it in any reasonable way.
You may try for something like this:
String.Format("{0:#,###0}", 0);
or may be like this:
string str = yourNumber.Remove(",").Replace(".",",");
Close enough tronc,
Try this snippet:
String curStr = "12.95";
Decimal decVal;
var valid = Decimal.TryParse(curStr, out decVal);
if (!valid) throw new Exception("Invalid format.");
String newFormat = decVal.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"));
Within the toString(...) call, you can append a number after 'C' to specify how many decimal places should follow. E.g "C3".

parsing a string value to double with comma decimal seperator

if there is any one who find a solution to this
string x = "7,50";
string y = "5";
double a = double.Parse(x);
double b = double.Parse(y);
double c = a - b;
then the result must be 2,50.
but I got 70. because of decimal point x is treated as 75.
Just specify the appropriate culture to double.Parse. For example:
CultureInfo french = new CultureInfo("fr-FR");
double x = double.Parse("7,50", french);
I suspect you actually had "7,5" as a value, however - as "7,50" would be parsed as "750" if you were using a culture which didn't use comma as the separator.
Of course, if these are currency values you should consider using decimal instead of double to start with...

Categories

Resources