How to Parse a String to Double - c#

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));

Related

How do I trim the "0." after I do modulo 1 on a double variable

Hello everyone as the title say I want to trim the "0." after I do modulo 1 on a double variable
Example:
double Number;
Number = Convert.ToDouble(Console.ReadLine()); //12.777
test = Number % 1; //0.777
I want my output to be: 777
only using math with no
string trims and so...
Thank you all !!
and in c# please
That is just a formatting on the ToString. Take a look at all your options here
How about
.ToString(".###");
Without using any string functions!
while(Math.Round(Number-(int)Number,1)!=1)
{
Number=Number/0.1;
if(Number-(int)Number==0)break;//To cover edge case like 0.1 or 0.9
}
NOTE: Number should be of double type!
If I take your question literally, then you do not want the decimal point either, so .ToString(".###") will not get you what you want, unless you remove the first character (which is string manipulation, and you said you don't want that either).
If you want 777 in a numeric variable (not a string), then you can multiply your result by 1000, though I don't know if you'll always have exactly 3 digits after the decimal or not.
The easiest way really is just to use string manipulation. ToString the result without any formatting, then get the substring starting after the decimal. For example:
var x = (.777d).ToString();
var result = x.SubString(x.IndexOf('.') + 1);
You are certainly looking for this:-
.ToString(".###");
As correctly pointed by Marc in comments you should have everything to be in a string, because if you output that 0.777 as it really is stored internally, you'd get 8 random bytes.
Something like this:-
var num = (.777d).ToString();
var result = num.SubString(num.IndexOf('.') + 1);
The most generic way to do this would be:
using System.Globalization;
var provider = NumberFormatInfo.InvariantInfo;
var output = test.ToString(".###", provider)
.Replace(provider.NumberDecimalSeparator, String.Empty);
You can also set the NumberDecimalSeparator on a custom NumberFormatInfo, but if you set it to empty it will throw the exception "Decimal separator cannot be the empty string."

Convert double to string, keep format - C#

Hi all. I have a double number (Ex: 0.000006). I want convert it to string type. But result is "6E-06". I dont want it, i want 0.000006".Thanks you so much
double a = 0.000006;
string resultString = a.toString();
I don't know many number after "." character
It's simple that if you want to show a number as exactly as what it looks, we can cast it to decimal and use the default ToString() like this:
var s = ((decimal)yourNumber).ToString();
//if yourNumber = 0.00000000000000000000000006
//just append the M after it:
var s = (0.00000000000000000000000006M).ToString();
Please check this article and find the format which suits your needs: http://www.csharp-examples.net/string-format-double/
Looks like this one is good enough:
String.Format("{0:0.00}", 123.4567);
Use String.Format() with the format specifier.
double a = 0.000006;
string formatted = String.Format("{0:F6}", a);
Try with the Roundtrip 'R' format specifier:
double a = 0.000006;
string resultString = a.ToString("R");
Double Rate_USD = Convert.ToDouble(txtRateUsd.Text);
string Rate_USD = txtRateUsd.Text;

double to certain string format

I have this simplified method:
private string GetStringValue(object Value)
{
return ((double)Value).ToString();
}
which spews out:
1.8E-09
I intend to get this format though:
1.8e-009
Is this easily achievable?
Looking at the documentation for custom numeric format strings, I think you want:
// Separate variable just for clarity
double number = (double) Value;
return number.ToString("0.###e+000");
(Use 0.###E-000 if you only want the symbol for negative exponents.)
You need to use String.Format and use the right format string.
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
This should help with decimal format strings.
So
(double)Value.ToString("E")
would do it for en-US.
return string.Format("{0:0.###E+000}", value);

Formatting a string number

I want to format a string number. For example:
double number="118176";
It should look like 1181.71 or 1181,71.
I couldn't find any format type. I tried some of format types as ToString("#,0") but it didn't work.
Thanks for any advice.
First of all: a double variable can not take a string. But that aside, something like this should help:
double number = 1181.76;
string output = String.Format("{0:d2}", number);
This takes the number and creates a string from it using the decimal number format with 2 decimal places.
What you didn't say is why you expect the integer 118176 to magically turn into a double with two decimals? The only way would be
double number = 118176;
string output = String.Format("{0:d2}", number / 100.0);
EDIT
Doing what you describe in your comment is a bit more complex:
string priceString = nodeFareList.SelectSingleNode("GenQuoteDetails/TotAmt").InnerText;
double priceDouble = Convert.ToDouble(priceString) / 100.0;
price.InnerHtml += String.Format("{0:c}", priceDouble);
This converts the number in priceDouble to a string with the value formatted like a currency. If you do not want the currency symbol, use the following:
price.InnerHtml += String.Format("{0:d2}", priceDouble);
See Fixed-point on this page (assuming .net): http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
You'll have to divide by 100 first though.
I think, what you are looking for is this:
String.Format("{0:0,0.0}", 12345.67);
String.Format("{0:0,0}", 12345.67);
See here for details.
Use can refer the below code in case of Java.
1) First define the Decimal Format instance.
DecimalFormat decimalFormat = new DecimalFormat("####.##");
2) Then pass the decimal value to the 'format' method
String dummyString = decimalFormat.format(pressureValue).toString()
where pressureValue - Decimal number
Hope this will be useful.

Parsing a string

I want to parse a string to long the value is 1.0010412473392E+15.But it gives a exception input string was not in a correct format.how to do this.
Both these answers work how to select both of them as answer.
Check out the System.Globalization.NumberStyles enumeration in the appropriate overload of Int64.Parse. If you specify System.Globalization.NumberStyles.Any, it should work:
long v = Int64.Parse(s, System.Globalization.NumberStyles.Any);
Note, however that the number you are parsing has limited precision, (there are only 13 decimal places but is specified as E+15). Also, the 'Any' enumeration is probably more than you really need - in this case you only need AllowDecimalPoint and AllowExponent:
long v = Int64.Parse(s, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowExponent);
Are you sure you don't want to parse to double?
var myDouble = double.Parse(myString);
You can then try converting to long.
var myLong = Convert.ToInt64(myDouble);

Categories

Resources