Why do i loose the decimal when parsing a string to decimal. ?[Answered] RSS

14 replies

Last post Feb 17, 2015 04:24 PM by robby32

• robby32

Member

280 Points

1000 Posts

Why do i loose the decimal when parsing a string to decimal. ?

Hi ,

i have the foillowing string "117.50" and I try to convert to a decimal and get 11750.

I am doing the following

string s= "117.50"

Convert.Todecimal(s);

result is 11750

expected 117.50

thanks

• oned_gk

All-Star

52493 Points

15665 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Try replace dot with comma before convert

Programming to simplify, don't look for difficult way
• gerrylowry

Star

14297 Points

5797 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

@robby32    Suwandi's suggestion is likely to work for you ...

this implies that your system settings are likely wrong since you expect a period to be your decimal point.

For that reason it appears that you've accidentally set the comma as your decimal point.

https://msdn.microsoft.com/en-us/goglobal/bb688127.aspx "Globalization Step-by-Step: Number Formatting"

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• robby32

Member

280 Points

1000 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Yep the comma is there because i have set my locale to Croatian for testing.

Now when i do calculation , i convert to decimal and get the number back . so for example my result is 11750-1750 = 10000

now this is \$100.00 dollars but in Croatian format it should be 100,00(is that correct ? )  but instead I get  "10.000,00" when i apply the following

var temAmpount =  10000;

tempOtherAmount.ToString("N" + Decimals.ToString());   ------->  gives "10.000,00" instead of 100.00 for Croatian format

thanks

• PaulLinton

Member

141 Points

55 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

You have set temAmpount to ten thousand (an integer).

Then you get the string representation of tempOtherAmount and get ten thousand (with two decimal places).

Is there a relationship between temAmpount and tempOtherAmount? Is one supposed to be one hundredth of the other?  Can you show your calculations?

• oned_gk

All-Star

52493 Points

15665 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Can you show complete codes?

Below code, i guess wrong

`tempOtherAmount.ToString("N" + Decimals.ToString());`

Do you mean?

`(tempOtherAmount + Decimals).ToString("N");`

Programming to simplify, don't look for difficult way
• gerrylowry

Star

14297 Points

5797 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

i suggest you use the Decimal.TryParse Method (String, NumberStyles, IFormatProvider, Decimal)

You need to be specific, depending on your globalization settings.

Study this code:

```System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
// Croation
Boolean goodA;
Boolean goodB;
String amountA = "117,50";  // pretend this is input from a Croatian
String amountB =  "17,50";  // pretend this is input from a Croatian
Decimal amountAparsed;
Decimal amountBparsed;
Decimal result;
style   = System.Globalization.NumberStyles.AllowDecimalPoint;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("hr-HR");
goodA = Decimal.TryParse(amountA, style, culture, out amountAparsed);
goodB = Decimal.TryParse(amountB, style, culture, out amountBparsed);
if(goodA && goodB)
{
result = amountAparsed - amountBparsed;
Console.WriteLine("{0} - {1} = {2}", amountAparsed, amountBparsed, result);
Console.WriteLine("{0} - {1} = {2}", amountAparsed.ToString(culture), amountBparsed.ToString(culture), result.ToString(culture));
}```

output:

```117.50 - 17.50 = 100.00
117,50 - 17,50 = 100,00```

Study this MSDN article:
https://msdn.microsoft.com/en-us/library/ew0seb73(v=vs.110).asptx "Decimal.TryParse Method (String, NumberStyles, IFormatProvider, Decimal)"

You will likely find this useful:  http://www.csharp-examples.net/culture-names/

edit:

Note:  internally, numbers and dates are stored the same, regardless of culture; you only need to give thought to the relevance of culture to your application with regards to parsing input and formatting output.

N.B.:  the Boolean result of a .TryParse must be true, otherwise, do not dare to use the out value.

end edit.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• robby32

Member

280 Points

1000 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

sorry tempothermount is meant to be tempamount , a typo

• robby32

Member

280 Points

1000 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Hi,

if lets says decimals is 2 , that i want to represent the number of decimal places, so i dont think that would be correct ,

• oned_gk

All-Star

52493 Points

15665 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

```        decimal d = 2;
Response.Write(d.ToString("0:0.00"));```

Programming to simplify, don't look for difficult way
• robby32

Member

280 Points

1000 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Thanks I studied the links put by

gerrylowry

they helped .

cheers

• gerrylowry

Star

14297 Points

5797 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

(a) you want to restrict the input to 2 decimal places?

(b) you want 2 decimal places in the output?

(c) both (a) and (b)?

Also, if your input is from a Croatian locale, do you intend to output in the format of that locale?

it might help if you tell us the salient points about your project with respect to this thread's O.P.

always study the remarks in MSDN articles to glean important information.  For example, from the above link you learn:

The Decimal value type represents decimal numbers ranging from

positive 79,228,162,514,264,337,593,543,950,335 to

negative 79,228,162,514,264,337,593,543,950,335.

The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors.

The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.

robby32, GIGO (Garbage in, Garbage out) ... if you want good data, you must edit your input.  if globalization is supported by your application, then you must edit your input with regards to the locale.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• robby32

Member

280 Points

1000 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

Hi,

Yes i want to 2 decimal places in the output . So if they give 5 decimal places , it will be rounded to 2.

" GIGO (Garbage in, Garbage out) ... if you want good data, you must edit your input.  if globalization is supported by your application, then you must edit your input with regards to the locale."

Thanks

• gerrylowry

Star

14297 Points

5797 Posts

Re: Why do i loose the decimal when parsing a string to decimal. ?

The .NET Framework has methods for rounding, and for controlling how one rounds.

Note in the links given that in some cases i've used .NET 4 because some of the .NET 4.5 links appear to be broken.

also, Google is your best friend ... i suggest you learn more about rounding, including mid-point rounding so that you can choose what is best for any given situation.

the default is "to Even" but imho it's best to be explicit for other person's who have to maintain your code.

```System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
// Croation
Boolean goodA;
Boolean goodB;
String amountA = "117,54567";  // pretend this is input from a Croatian
String amountB =  "17,54499";  // pretend this is input from a Croatian
Decimal amountAparsed;
Decimal amountBparsed;
Decimal result;
style   = System.Globalization.NumberStyles.AllowDecimalPoint;
culture = System.Globalization.CultureInfo.CreateSpecificCulture("hr-HR");
goodA = Decimal.TryParse(amountA, style, culture, out amountAparsed);
goodB = Decimal.TryParse(amountB, style, culture, out amountBparsed);
if(goodA && goodB)
{
result = amountAparsed - amountBparsed;
Console.WriteLine(" no rounding {0} - {1} = {2}", amountAparsed, amountBparsed, result);
Console.WriteLine(" no rounding {0} - {1} = {2}", amountAparsed.ToString(culture), amountBparsed.ToString(culture), result.ToString(culture));
Decimal amountArounded = Math.Round(amountAparsed, 2, MidpointRounding.AwayFromZero);
Decimal amountBrounded = Math.Round(amountBparsed, 2, MidpointRounding.AwayFromZero);
result = amountArounded - amountBrounded; // note that result is NOT rounded
Console.WriteLine("    away from zero {0} - {1} = {2}", amountArounded, amountBrounded, result);
Console.WriteLine("    away from zero {0} - {1} = {2}", amountArounded.ToString(culture), amountBrounded.ToString(culture), result.ToString(culture));
amountArounded = Math.Round(amountAparsed, 2, MidpointRounding.ToEven);
amountBrounded = Math.Round(amountBparsed, 2, MidpointRounding.ToEven);
result = amountArounded - amountBrounded; // note that result is NOT rounded
Console.WriteLine("           to even {0} - {1} = {2}", amountArounded, amountBrounded, result);
Console.WriteLine("           to even {0} - {1} = {2}", amountArounded.ToString(culture), amountBrounded.ToString(culture), result.ToString(culture));
amountArounded = Math.Round(amountAparsed, 2);
amountBrounded = Math.Round(amountBparsed, 2);
result = amountArounded - amountBrounded; // note that result is NOT rounded
Console.WriteLine("default is to even {0} - {1} = {2}", amountArounded, amountBrounded, result);
Console.WriteLine("default is to even {0} - {1} = {2}", amountArounded.ToString(culture), amountBrounded.ToString(culture), result.ToString(culture));
}```

output:

``` no rounding 117.54567 - 17.54499 = 100.00068
no rounding 117,54567 - 17,54499 = 100,00068
away from zero 117.55 - 17.54 = 100.01
away from zero 117,55 - 17,54 = 100,01
to even 117.55 - 17.54 = 100.01
to even 117,55 - 17,54 = 100,01
default is to even 117.55 - 17.54 = 100.01
default is to even 117,55 - 17,54 = 100,01```

.NET 4   https://msdn.microsoft.com/en-us/library/3s2d3xkk(v=vs.100).aspx "Math.Round Method (Decimal, Int32)"

.NET 4  https://msdn.microsoft.com/en-us/library/ms131275(v=vs.100).aspx  "Math.Round Method (Decimal, Int32, MidpointRounding)"

likewise, do study the code examples on the MSDN pages whenever they are present.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• robby32

Member

280 Points

1000 Posts