I have a null able double value which get values from data base.It retrieve value from data base as '1E-08'. I want to display the value with out scientific notification (0.00000001)
I used the following code.
double? valueFromDB=1E-08;
string doubleValue=valueFromDB.Value.Value.ToString();
string formatedString=String.Format("{0:N30}", doubleValue);
But the value of formatedString is still 1E-08.
You're calling string.Format with a string. That's not going to apply numeric formatting. Try removing the second line:
double? valueFromDB = 1E-08;
string formattedString = String.Format("{0:N30}", valueFromDB.Value);
Or alternatively, specify the format string in a call to ToString on the value:
double? valueFromDB = 1E-08;
string formattedString = valueFromDB.Value.ToString("N30");
That produces 0.000000010000000000000000000000 for me.
Related
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);
I have below code :
private string Do2Decimal(string value)
{
return String.Format("{0:0.##}", value);
}
Here I am passing string value as 16.32222.
What I know is, above code should format my value to 16.32.
But it is showing output value as 16.32222.
What mistake I am doing here??
Update :
I have values in string format that is : "16.32222".
Sorry forgot to mention before.
Because you are passing it a string value. The formatting would work for floating point numbers / decimals. Just parse the number to either decimal / double type depending on your requirement like:
String.Format("{0:0.##}", decimal.Parse(value));
You can also use decimal.TryParse (or TryParse) family method for safer parsing. You can also modify your method to receive decimal/double type parameter and then apply the formatting. It would convey a better intent, IMO.
If your string has . as NumberDecimalSeparator and your culture doesn't support . as NumberDecimalSeparator then you can pass CultureInfo.InvariantCulture while parsing like:
string value = "16.322222";
string formattedString = String.Format("{0:0.##}", decimal.Parse(value, CultureInfo.InvariantCulture));
Try this
String.Format("{0:0.00}", decimal.Parse(value));
to get two degit value after point.
Doing this:
double dblRateEvalResult = -0.52;
string strNewResult = dblRateEvalResult.ToString("000.####").TrimStart('-');
I want:
000.5200
I get:
000.52
What am I doing wrong?
You need 0 custom specifier instead of #
string str = dblRateEvalResult.ToString("000.0000").TrimStart('-');
(In your code you are trying to assign string to a double value, I guess this is a typo)
See: The "0" custom format specifier - Custom Numeric Format Strings
If the value that is being formatted has a digit in the position where
the zero appears in the format string, that digit is copied to the
result string; otherwise, a zero appears in the result string.
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");
Here is my string
20.0e-6
I'm parsing it like
String Ans=Double.Parse("20.0e-6")
Now i'm getting the result like 2E-05
But the required output should be like
0.00002
How to get this?
The result of Double.Parse is a Double, not a string. You need to output a string from the double, using ToString.
You should also use an overload of Double.Parse that has a NumberStyles parameter. Using the Float value allows exponent notation:
string Ans=Double.Parse("20.0e-6", NumberStyles.Float).ToString("0.#####");
If you don't want to risk exceptions (InvlidCastException for example), you can use TryParse:
Double res;
if (Double.TryParse("20.0e-6", NumberStyles.Float,
CultureInfo.InvariantCulture ,res))
{
string Ans = res.ToString("0.#####");
}
It's the same number, but if you want to modify the output of the string, use a formatter on your ToString()
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
So
String Ans=Double.Parse("20.0e-6").ToString("0.0####")
One way to get the result you want is to use String.Format as follow:
double x = 20.0e-6;
string y = string.Format("{0:0.######}",x);
Console.WriteLine(y);
Given your example, this outputs the value 0.00002
EDIT
I've just realised that this is actually the opposite of your question so in the aim of keeping the answer useful i'll add the following:
Given a string, you can parse as double and then apply the same logic as above. Probably not the most elegant solution however it offers another way to get the result you want.
string x = "20.0e-6";
var y = double.Parse(p);
Console.WriteLine(String.Format("{0:0.######}",y));