Convert a very Small number String to Decimal [duplicate] - c#

This question already has answers here:
Convert from scientific notation string to float in C#
(2 answers)
Closed 9 years ago.
I want to convert a very Small number to Decimal,
Lets Say
String secondsStr = 0;
Decimal secondsValue;
Boolean success = Decimal.TryParse(secondsStr, out secondsValue);
But the problem is I have the string representation of it, 3.24E-08
String secondsStr = 3.24E-08;
Decimal secondsValue;
Boolean success = Decimal.TryParse(secondsStr, out secondsValue);
It always return success as false.
How can I parse that to get 0.00000003244657 ?

You can use TryParse with the NumberStyles argument:
var ok = Decimal.TryParse(secondsStr, NumberStyles.Any, null, out secondsValue);
I have used NumberStyles.Any which works.
Indicates that all styles except AllowHexSpecifier are used. This is a
composite number style.
Update: if it works with NulberStyles.Float depends on the current culture. If it uses . as decimal separator it works. So you can also use CultureInfo.InvariantCulture as third argument:
var ok = Decimal.TryParse(secondsStr, NumberStyles.Float, CultureInfo.InvariantCulture, out secondsValue);

Try this
Boolean c= decimal.TryParse("3.24E-08", System.Globalization.NumberStyles.Float, null, out a);

Related

How do I write a small number to the console without the e notation in C#? [duplicate]

This question already has answers here:
How to get floats value without including exponential notation
(5 answers)
Double to string conversion without scientific notation
(18 answers)
Closed 2 years ago.
When I try to display a very small or a very big number, it shows us the number with the e notation.
How do I bypass this issue?
Things I have tried:
Console.WriteLine(Double.Parse("1E-10", System.Globalization.NumberStyles.Float));
/* System.FormatException: Input string was not in a correct format.
* at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
* at Rextester.Program.Main(String[] args)
*/
Console.WriteLine(Convert.ToString(Math.Pow(10,-10)));
// returns 1E-10
double num = Math.Pow(10,-10);
Console.WriteLine(num.ToString());
// returns 1E-10
For this one you need to use a string format. For your case Pow(10, -10) will return 0.0000000001, so you will need to use a format that can display 10 decimal places.
You may declare a string format like this:
const string format = "0.##########";
Then use: Console.WriteLine(num.ToString(format));

TextBlock string format with dot WPF [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?
Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}
You can try like this:
string myFormatedString = number.ToString("+#;-#");
The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

Format a number to always have a sign and decimal separator [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?
Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}
You can try like this:
string myFormatedString = number.ToString("+#;-#");
The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

Decimal to String Formatting [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 9 years ago.
I try to convert (Decimal)0.9975 to string with (0.##) format in C# but it rounds the number to 1 instead of 0.99
Here is the code;
decimalValue.ToString("0.##");
How can I write the output as 0.99?
I got this on SO long time back. I too was struck with something similar. I owe this post to him.
decimal d = 0.9975m;
decimal newDecimal = Math.Truncate((d*100))/100;
string result = string.Format("{0:N2}", newDecimal.ToString()); // OR
string result = newDecimal.ToString(); //This is simpler I guess.
Hope it helps.
the other option is to accept the rounding but subtract 0.005 from the decimal
decimal d = 0.9975m;
string result = (d-0.005m).ToString("0.##");
(0.9975 - 0.005) = 0.9925;
0.9925 => 0.99
use format
decimalValue.ToString("#0.0#");
The '#' will be updated if there is a value on the placeholder, if there is no value on the'#' placeholder, then this will be ignored, but the '0.0' will not be ignored.
or
var value = string.Format("{0:0.00}", decimalValue);
or
decimal decimalValue = 0.9975;
value.ToString("G3");
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

How to round and format a decimal correctly? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I'm trying to get my decimals to display with four decimal places. The DB rounds my number to 4 decimal places, but it returns the number with trailing 0s (due to the decimal precision of the field), so something like 9.45670000. Then, when I do this:
string.Format("{0:#,#.####}", decimalValue);
The output I get on the page is 9.4567, which is what I want.
However, if the number returned from DB is 9.45600000, the output after doing the format is 9.456
But what I need to display is 9.4560
How do I format my decimal, so that the number of decimal places is always four?
UPDATE: Also, is it possible to use a variable (instead of .0000) if I wanted the number of decimal places to be determined dynamically?
string.Format("{0:N4}",decimalValue);
Standard Numeric Format Strings
Custom Numeric Format Strings
To set the precision dynamically you can do the following:
double value = 9.4560000;
int precision = 4;
string format = String.Format("{{0:N{0}}}",precision);
string valuestring = String.Format(format, value);
string.Format({0:#,#0.0000}, decimalValue);
Use String.Format -
decimal d =123.47
string specifier="{0:0,0.0000}"; // You need to get specifier dynamically here..
String.Format(specifier, d); // "123.4700"
Try this:
string.Format("{0:#,###.0000}", 9.45600000);
Adding the zeroes in the format forces a zero to be output if there is not a digit to put there.
To add the zeroes with the number of zeroes driven programmatically you could do this:
int x = 5;
string fmt = "{0:#,###." + new string('0', x) + "}";
string.Format(fmt, 9.456000000);

Categories

Resources