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...
Related
The value that extracted from the application is in string format for ex. "$0.38". So, I segregated each character in the given string using IsDigit then appended them together using string builder. The digit can also be alphanumeric like "12,365.23 AS". Is there a way to recover only numeric part (along with the decimal) from the given string.
But Output I receive is "38" instead of "0.38". I also want to compare that the given string value lies between the upperLimit and lowerLimit provided.
Please let me know how to proceed with the same.
string Value = "$0.38";
int upperLimit = 2500;
int lowerLimit = 50000;
StringBuilder sb = new StringBuilder();
//sb.Append(someString);
foreach (char amin in Value)
{
if (System.Char.IsDigit(amin))
{
sb.Append(amin);
}
}
int compareVal = Convert.ToInt32(sb.ToString());
Console.WriteLine("value for comparision" + " " + compareVal);
The best way is using one of the overloads of decimal.Parse:
string Value = "$0.38";
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
decimal dd=decimal.Parse(Value, System.Globalization.NumberStyles.AllowCurrencySymbol|System.Globalization.NumberStyles.AllowDecimalPoint,culture);
Note the use of NumberStyles enum.That way you can control exaclty the parsing.
There are two reasons why you will get 38:
StringBuilder looks like "038", since "." is not a digit (just like "$").
Convert.ToInt32(...) returns an integer which doesn't allow decimal digits.
The better data type for currencies is decimal, a high precision floating point data type so to say.
Try
var amount = decimal.Parse(Value , NumberStyles.Currency)
var isInLimit = upperLimit <= amount && amount <= lowerLimit; // i guess you swapped upper and lower limit btw. ;)
instead.
Edit
In order to use the NumberStyles-Enumeration, you will have to use tha correct namespace in your file:
using System.Globalization;
You are omitting the decimal point and you are not using a decimal data type to hold the converted value. The real way to go is to convert the currency string to a decimal number:
CultureInfo usCulture = new CultureInfo("en-US)";
decimal amount = decimal.Parse(Value, NumberStyles.Currency, usCulture);
You can then perform a proper numeric comparison:
if (amount <= upperLimit && amount >= lowerLimit)
....
I first marked the question as a duplicate, but then changed my mind. I still think it is very much related to: Convert any currency string to double
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 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 can I do two things in tostring format:
1. Display number to x decimal places
2. Also force a + sign so that negative an positive numbers lineup in nice columns.
string fmt = "+N" + dp + ";-N" + dp;
Console.WrtieLine(Open.ToString(fmt))
Does not work?
You'll need to create a custom format string to do what you want. http://msdn.microsoft.com/en-us/library/0c899ak8.aspx tells you about these but the below is a quick example of one thing that does the trick.
double num = 1111.019123;
int dp = 2;
string format = String.Format("+#.{0};-#.{0}",new string ('#',dp));
Console.WriteLine(num.ToString(format));
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");