Answered by:
Decimals not working well
Question

Hi there!
This is my first post here, so if there are related posts with the same question or whatever 'm sorry, I did a little search and I didn't found anything.
I've created my first software with windows forms using Visual C++ 2010 (a little insuline calculator I made for an hospital) but I've got an issue with the decimals. My problem is that if you use numbers with different numbers of decimals (for example, inserting 10.8 and 2.50 in the textboxes inputs), the answer is not correct. You have to use 10.8 and 2.5 or 10.80 and 2.50.
Variables nivact and nivdes could be integers, I put them decimals just to test. So the issue is in the carbfactor and carbos variables.
private: System::Void textBox3_TextChanged(System::Object^ sender, System::EventArgs^ e) { //blabla not related code here
else { Decimal insulina = 0; Decimal nivact = Decimal::Parse(textBox3>Text); nivact = Math::Round(nivact, 1); if(nivact > 0) { Decimal nivdes = Decimal::Parse(textBox4>Text); nivdes = Math::Round(nivdes, 1); if (Decimal::Compare( nivdes, nivact ) < 0) { Decimal senfactor = Decimal::Parse(textBox7>Text); senfactor = Math::Round(senfactor, 1); if (Decimal::Compare( senfactor, 0 ) > 0) { Decimal carbfactor = Decimal::Parse(textBox10>Text); //carbfactor = Math::Round(carbfactor, 1); if (Decimal::Compare( carbfactor, 0 ) > 0) { Decimal carbos = Decimal::Parse(textBox8>Text); //carbos = Math::Round(carbos, 1); if (Decimal::Compare( carbfactor, 0 ) >= 0) { Decimal dif = nivact  nivdes; dif = Math::Round(dif, 1); insulina = Decimal::Divide( dif, senfactor ); Decimal carbosplus = Decimal::Divide( carbos, carbfactor );//carbos/carbfactor; carbosplus = Math::Round(carbosplus, 1); insulina = insulina+carbosplus; } } } } } if (insulina <= 0) textBox9>Text = "error"; else { Decimal d = Math::Round(insulina, 1); textBox9>Text = d.ToString(); } } }
I've tried to use doubles and to round every number to the same number of decimals, but it isn't working, answer it's not okay if both numbers (carbos and carbfactor) have the same number of decimals.
Any advice would be very appreciated
Thanks in advance,
Luthor
Thursday, February 16, 2012 9:10 PM
Answers

LuthorMithos wrote:>>The code is just a small calculator of the quantity of insuline you have>to inject depending on some variables. The problem only appears dealing>with decimals. And there's no pattern in the fail, at least not any I>can see after some debugging :S.I do notice that your values are using a decimal point (2.5) but the resultof your generated value has a comma (2,7). I suspect a localizationscrewup. Are the results better if you use commas when entering yourvalues?Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc. Marked as answer by LuthorMithos Friday, February 17, 2012 8:31 AM
Friday, February 17, 2012 5:49 AM
All replies

... but I've got an issue with the decimals. My problem is that if you use numbers with different numbers of decimals (for example, inserting 10.8 and 2.50 in the textboxes inputs), the answer is not correct.
Thursday, February 16, 2012 11:43 PM 
I rounded the decimals just to test, actually I only need a single number rounded, the last "else" that appears in the code (which is the number that appears in the textbox "Quantitat d'insulina recomanada")(see image below).
The code is just a small calculator of the quantity of insuline you have to inject depending on some variables. The problem only appears dealing with decimals. And there's no pattern in the fail, at least not any I can see after some debugging :S. Maybe I could upload the debug version if it may help.
I've take a couple of pics, let's see if you guys can understand my problem with them, it's kind of weird...
In the left, the real answer. It's fine when carbfactor and carbos have the same number of decimals. On the other hand, in the right, bad answer with different number of decimals.
Thanks,
Luthor
Friday, February 17, 2012 12:21 AM 
LuthorMithos wrote:>>The code is just a small calculator of the quantity of insuline you have>to inject depending on some variables. The problem only appears dealing>with decimals. And there's no pattern in the fail, at least not any I>can see after some debugging :S.I do notice that your values are using a decimal point (2.5) but the resultof your generated value has a comma (2,7). I suspect a localizationscrewup. Are the results better if you use commas when entering yourvalues?Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc. Marked as answer by LuthorMithos Friday, February 17, 2012 8:31 AM
Friday, February 17, 2012 5:49 AM 
Goddamn thing! I'm so used to use point as decimal separator that I forget that in Spain the separator is the coma!
Thanks for everything, everything was working well, that was the only problem ><
Regards,
Luthor
Friday, February 17, 2012 8:34 AM