Error While Using Math.Round - c#

i am working on a problem to round decimal values everything is fine but at one certain point it gives me error input string was not formatted after debugging my code i found 4.90702817E-05 this which gives me error on this calling code . It converts all code but throws exception when it finds above string .
MarketValue = Convert.ToDecimal(row["MarketValue"].ToString()) ,

MarketValue = Decimal.Parse(row["MarketValue"].ToString(), NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);

Convert to Decimal by using below code. After getting the Value in Decimal form round it up.
decimal d = Decimal.Parse("4.90702817E-05", System.Globalization.NumberStyles.Float);

The reason for the exception could be that the value is not a valid decimal.
The following code uses a method called TryParse(...). This method returns false if the value is not a valid decimal, rather than throwing an exception.
For example:
decimal d = 0;
if (Decimal.TryParse(row["MarketValue"].ToString(), out d))
{
MarketValue = d;
}

I'd advise you to use TryParse using following NumberStyles
decimal d = 0;
if (Decimal.TryParse(row["MarketValue"].ToString(), NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out d))
{
MarketValue = d;
}

Related

How to convert the double value to string without losing the data in c#

I have a double value as below
double propertyValue = 1994.7755474452554;
When i convert this double value to a string
string value = propertyValue.ToString();
it gives the value "1994.77554744526"
It has rounded off the last digits 2554 to 26.
But I do not want to round the value. Share your ideas.
By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17" format specifier to the method.
String s = value.ToString("G17");
This will prevent the rounding off of double value when converted to string.
value = propertyValue.ToString("G17");
You could use decimal type instead.
decimal propertyValue = 1994.7755474452554M;
I can't reproduce the misbehaviour; you've experienced a representation effect: it's ToString() puts double like that.
string source = "1994.7755474452554";
double d = double.Parse(source, CultureInfo.InvariantCulture);
string test = d.ToString("R", CultureInfo.InvariantCulture);
Console.Write(source.Equals(test) ? "OK" : "Rounded !!!");
Outcome is OK. Please, notice "R" format string:
The Round-trip ("R") Format Specifier
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString
You can cast double to decimal and use the default ToString() like this:
Double propertyValue = 1994.77554744526;
var str = ((decimal)propertyValue).ToString();
//str will hold 1994.77554744526

unable to do type casting string to int in C# , giving error - Input string was not in a correct format

I want to convert a string into int. I have string value like this "45,454,566.00". While typecasting throwing error
Input string was not in a correct format
int GrandTotalInWords = Int32.Parse(grandtotal);//error
grandTotalInWords.InnerHtml = ConvertNumbertoWords(GrandTotalInWords);
First of all, int.Parse uses NumberStyles.Integer which does not includes any thousands and decimal separator styles.
As a second, your CurrentCulture might not use , as a NumberGroupSeparator and/or might not use . as a NumberDecimalSeparator.
Just specify your culture settings and number styles;
int GrandTotalInWords = int.Parse(grandtotal,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
CultureInfo.InvariantCulture);
By the way, if your fraction part is not always zero, that makes your string is not a valid format for an int. In such a case, you might need to parse it to a floating-point type like double as;
double GrandTotalInWords = double.Parse(grandtotal,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
CultureInfo.InvariantCulture);

Convert from exponential notation to non-exponential notation

I have tried:
MessageBox.Show(System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture));
But it continues to show 7.56e+011
You are looking to format the number. You can use String.Format to do so
string.Format("{0:F}",System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture))
Running the following code
Gives you the following MessageBox
you can specify no decimal points by changing it to {0:F0} for the format.
Try
BigInteger num = System.Numerics.BigInteger.Parse("7.56e+011",
NumberStyles.Float,
CultureInfo.InvariantCulture);
String text = num.ToString("F5"); // New format string, here with 5 digits.
Your solution does an implicit conversion from BigInteger back to string again, which uses the scientific notation if the exponent is large.
decimal dec = decimal.Parse("7.7583877127496407E-6",
System.Globalization.NumberStyles.Any);
Console.WriteLine(dec);

unable to understand decimal in C#

I try:
decimal dd = 4.12345611111111111;
lblText.Text = string.Format("{0:N6}", dd);
but I got a error use an M suffix
decimal dd = 4.12345611111111111m;
its work perfect.I want to know what is m for?.Thanks for clearing my vision.
note: lblText is id of label control in asp.net.
If you don't specify the suffix the default is double for a number with decimal separator. M specify that the literal is actually a decimal
It is a literal used for decimal type. Read more here
TO display decimal value you have to append m or M.
decimal d = 1.04893m;
The m stands for monetary if I'm not mistaken, the f for float, and the d, surprisingly, for double.

Convert "2.45" to decimal

I know this is the silliest question to ask. But really I'm having trouble to convert price(string) to decimal. Here is what I have tried
string s = "123.45";
decimal d = decimal.Parse(s, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint);
But I'm getting Format exception
Also tried
decimal num;
bool pass = decimal.TryParse("2.85", out num);
num comes out as 0.
Any help will be greatly appreciated.
Try specifying an invariant culture in which . is the decimal separator:
string s = "123.45";
decimal d = decimal.Parse(
s,
NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture
);
The Parse method uses the current thread culture to parse numbers. So if you are using some culture in which . is not the decimal separator (such as fr-FR for example) it won't work as it would expect the number to be 123,45 for instance.

Categories