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
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
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
Yes i want to 2 decimal places in the output . So if they give 5 decimal places , it will be rounded to 2.
I realise your point
" 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."
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
Note: in addition to reading the Remarks, it's also often useful to read any Community Additions on the MSDN documentation pages.
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
Member
280 Points
1000 Posts
Why do i loose the decimal when parsing a string to decimal. ?
Feb 16, 2015 11:07 PM|robby32|LINK
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
All-Star
52493 Points
15665 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 16, 2015 11:52 PM|oned_gk|LINK
Try replace dot with comma before convert
Suwandi - Non Graduate Programmer
Star
14297 Points
5797 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 12:04 AM|gerrylowry|LINK
@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"
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 12:10 AM|robby32|LINK
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
Member
141 Points
55 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 12:41 AM|PaulLinton|LINK
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?
All-Star
52493 Points
15665 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 01:25 AM|oned_gk|LINK
Can you show complete codes?
Below code, i guess wrong
Do you mean?
Suwandi - Non Graduate Programmer
Star
14297 Points
5797 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 02:31 AM|gerrylowry|LINK
@robby32
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:
output:
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.
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 03:38 AM|robby32|LINK
sorry tempothermount is meant to be tempamount , a typo
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 03:42 AM|robby32|LINK
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 ,
All-Star
52493 Points
15665 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 03:53 AM|oned_gk|LINK
Suwandi - Non Graduate Programmer
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 04:09 AM|robby32|LINK
Thanks I studied the links put by
gerrylowry
they helped .
cheers
Star
14297 Points
5797 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 04:21 AM|gerrylowry|LINK
@robby32
Please clarify. Are you saying
(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.
robby32, you will discover that carefully studying this https://msdn.microsoft.com/en-us/library/system.decimal(v=vs.110).aspx will help your understanding.
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.
Please read: http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer ... if your questions are not clearly and precisely stated, your peers here at forums.asp.net will experience substantial difficulty in giving you clear answers.
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 05:11 AM|robby32|LINK
Hi,
thanks so much for your help , i have read the link you provided
https://msdn.microsoft.com/en-us/library/system.decimal(v=vs.110).aspx
it was helpful.
Yes i want to 2 decimal places in the output . So if they give 5 decimal places , it will be rounded to 2.
I realise your point
" 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
Star
14297 Points
5797 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 11:42 AM|gerrylowry|LINK
@robby32
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.
output:
links:
.NET 4.5 https://msdn.microsoft.com/en-us/library/System.Math.Round(v=vs.110).aspx
.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)"
Note: in addition to reading the Remarks, it's also often useful to read any Community Additions on the MSDN documentation pages.
likewise, do study the code examples on the MSDN pages whenever they are present.
Member
280 Points
1000 Posts
Re: Why do i loose the decimal when parsing a string to decimal. ?
Feb 17, 2015 04:24 PM|robby32|LINK
Hi ,
thank you so much , it has been extremely helpful and I wil definitely bookmark these links ....much appreciated