1E-08 to decimal [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse a Number from Exponential Notation
Does Decimal.Parse() support scientific notation?
I am trying to convert such values as 1E-08 to a decimal in C# because decimals are the preffered datatype for handling funds yet I get an error upon decimal.Parse() "Input string was not in a correct format." wouldn't converting to float first and then to decimal defeat the purpose?

Yes, converting to float would indeed defeat the purpose. The good thing is, you don't have to do that here!
You can use an overload for Parse that takes a NumberStyles specifier:
decimal d = decimal.Parse("1E-08",
System.Globalization.NumberStyles.AllowExponent);
Of course, if you are merely specifying a hard-coded decimal, you can use the decimal literal format:
decimal d = 1E-08M;

You can try with :-
decimal x = decimal.Parse("1E-08", NumberStyles.Float);

Related

why does double return this value? C# [duplicate]

This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
decimal vs double! - Which one should I use and when? [duplicate]
(7 answers)
Closed 6 years ago.
double sth = 250 - 249.99;
Console.WriteLine(sth);
Why does this return sth like 0.009994507, instead of 0.01?
Floating point numbers (in this case doubles) cannot represent decimal values exactly. For more info, see this page here
If you need a more accurate representation, use decimal instead.
because when you print the double you print the all double value not just the first x after point digits.
you can use String.Format to print only the first 2 numbers.
double sth = 250.00d - 249.99d;
string sthString = String.Format("{0:0.00}", sth);
Console.WriteLine(sthString);
There are a lot of decimals that have infinite binary representation. What you're experiencing is exactly this case.
For more on this topic see: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

C# - wrong string to single conversion [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 6 years ago.
I am writing a program to get data from microcontroller to PC. The data is in float format. I tried to convert the string into float using Convert.ToSingle(string), but the conversion result is wrong:
"0.11" is converted to 11, sometimes 12.
"0.10" is converted to 10. etc
As you can see, it is losing the leading 0. , which is unexpected. How could this happen?
Your problem is culture specific. In some cultures float numbers are separated by a , and in some they are separated by a .
In your case
String a = "0,11";
Convert.ToSingle(a)
should result in your desired outcome of 0,11.
So you should explicitly specify a relevant culture that uses . as decimal separator. One possibility is the invariant culture which is based on the English language.
Try the following:
String a = "0.11";
Convert.ToSingle(a, CultureInfo.InvariantCulture)

C# issue with double number [duplicate]

This question already has answers here:
Difference between decimal, float and double in .NET?
(18 answers)
Closed 9 years ago.
I have a strange issue using double numbers in C# . NET here is my test:
double my_value = 0.49;
the problem is that the variable value shown is instead 0.48999999999999999 I do not need to display 0.49 using Math.Round() function; I need to exactly store this value.
Thank you.
Welcome to floating point precision. Use the decimal type if you want more precision.
decimal my_value = 0.49m;
If you want to learn more on why this is I recommend you read this article - What Every Computer Scientist Should Know About Floating-Point Arithmetic
Use decimal instead, double is floating binary point type.
decimal my_value = 0.49m;
Useful links;
Floating-Point Representation and Precision
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Do not use the double type when you need to use exact values. That is the domain of the decimal type
decimal my_value = 0.49m;

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Trimming a float [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Leave only two decimal places after the dot
Formatting a float to 2 decimal places
If I have a float that consists of something like 153.2154879, is there any way to convert it to string but only show 4 decimal places? I know I can format it using "000.000", but the front number doesnt always have to be 3 digits. So is there a way to show all the front numbers (153), but only the first 4 characters after the point in a string?
Something like this should do:
your_number.ToString("0.####");
This will show a max of 4 decimal places.
I usually use a format string like "#0.0000".
You can use the C# function Math.Round function.
float a= 153.213456;
Math.Round(a,3);
this would round up the number to 153.213
then get convert it to string.

Categories