Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / currency, Floating Point accuracy,

Author
Message
old man
20
Years of Service
User Offline
Joined: 25th Feb 2004
Location:
Posted: 21st Feb 2009 13:25
Hi everyone

Once again I am struggling with what should be a simple operation.

I store prices as pence in an array that is type float.

I have a quantity that is type float.

I multiply the two together and get the correct answer, eg 159.00000 x 2.0000000 = 318.00000

I now divide by 100 to bring to GBP

318.00000/100 = 3.1800001

I am not concerned about the difference being slightly over, but I want to print just to 2 dp, so I want £3.18 and so I could truncate to 3.18.

However This is a problem when answer slightly out the other way,
3.1799999 does not truncate ok.

I have looked at the currency help but I cannot see how to use it,( my knowledge of C++ tends towards zero).

I would really like to use currency so that I can easily use GBP, Euro or USD.

Help please!!

uff
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Feb 2009 14:17
Currency + Floating point = Don't do it.

Floating point maths is not designed for accuracy. Your numbers will NOT add up, as you are already finding, and using doubles only delays the problem for a while, so don't use those either.

Instead, you'll need to use integer maths and write your own display routines. As an example, if you had a value of £123.45, you might hold that as 1234500 if you wanted 4 decimal places. But also remember the limitations of integer - you have 9 full digits - so you may also want to use a long long which will give you 18 full digits.

old man
20
Years of Service
User Offline
Joined: 25th Feb 2004
Location:
Posted: 21st Feb 2009 16:34
Thanks IanM

Done that and it works fine, shame about all the lines of code but it works.

Is there any way of formatting a print in DarkGDK other than doing separate SetCursor for each part of my resulting £22.59 ??

I am currently doing two TextWidth plus four SetCusor and Print commands just to print £22.59 right adjusted.

uff
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Feb 2009 17:14
Here's an example for 4 decimal places:


old man
20
Years of Service
User Offline
Joined: 25th Feb 2004
Location:
Posted: 21st Feb 2009 17:53
Thanks IanM

That is far better, looked in the book and am now going to play around with sprintf.

Thanks again!!

uff

Login to post a reply

Server time is: 2024-11-25 15:42:57
Your offset time is: 2024-11-25 15:42:57