EPOS Java Number Rounding Problem

I am developing a java EPOS system and am having a strange issue with number accuracy that doesnt make any sense to me.

On a till system you have a user display of the form

0000000.00

When you type into this for example the number 7 then the number 8 it goes

0000000.07

0000000.78

Moving along the screen.

I cant figure out how to represent this accurately (ie using floats) but still have the functionality to append onto the float, not add to it.

i have come up with the following

Float tempNum = currentNum*10;

StringBuffer sb =new StringBuffer(tempNum.toString());

sb.append(i);

String s = sb.toString();

currentNum = Float.valueOf(s).floatValue();

updateOutputs();

With the basic idea being casting to a string buffer, appending then casting back of sorts but i get accuracy issues...

Pressing 7 a whole bunch of times gives 7777.9995 not 7777.77 etc

I know there must be a simple and more elegant way to solve this problem but i just cant seem to think around it right now.

Thanks

[1135 byte] By [Dezila] at [2007-10-2 7:51:05]
# 1
[url= http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html]How to Use Formatted Text Fields[/url]
yawmarka at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 2
Store your numbers in an into or long and divide them by 100 when you want to display them. Or indeed use a String and Double.parseDouble to get a value to calculate with.
CeciNEstPasUnProgrammeura at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 3
You can't have total accuracy using Floats or floats. Use BigDecimal instead.regards
akimotoa at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks for all the fast replies, really helped. Ive now got it working acurately which is great except when the integer is over 9 digits long i get some errors thrown.

This arrises when trying to convert the int to a stringbuffer. I assume the number is then too large so im just going to ignore input after 9 digits allowing the till to go up to 9,999,999.99 but how do you count the number of digits in an int? I think im just having a bad day here as i cant seem to figure it out. I have thought of converting it to a type that will allow me to do it but it seems really strange that i cant simply get a digit count?

Thanks

Dezila at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 5
Why do you need to count the digits? Either you have a text field, so limit the size of that, or you have a console, than you can quesry the input String's length.
CeciNEstPasUnProgrammeura at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 6
"then you can query"
CeciNEstPasUnProgrammeura at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 7
9 digits? then switch from int to long...if long is not sufficiently big, move to BigDecimal or BigInteger
jsalonena at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 8

This is an input panel on an EPOS system, but as im designing it for touch screen it has an on screen number pad and display of the number.

When the buttons are pressed for the numbers that number is appended to the internal integer storage value. Also the output string is updated.

Most till systems allow you to enter up to X digits until then ignore what you type or print an error, so i am happy to follow this behavior but require the digit count.

I hope that makes sense but if not my gui is similar to these...

http://www.tillrollshop.co.uk/asps/uploads/big/1205-1.jpg

http://www.mi-store.com/mi-store/Till_Main.jpg

The second image shows a number pad and label just like mine.

Thanks

Dezila at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 9
int numberOfDigits = (int) Math.log10(number) + 1
jsalonena at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 10
> int numberOfDigits = (int) Math.log10(number) + 1IMO the OP simply mixes up presentation and data model. If the user can't enter more than 9 digits, there's no need to check the data model.
CeciNEstPasUnProgrammeura at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 11
Its all working now, thansk guys.I understand it would be better if they could enter into a text box etc but im developing for touch screen so have to give them the option to enter what they want and then check it over. Thanks for all the fast help tho!
Dezila at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...
# 12
yeah but since he asked i thought i'd show off my excellence .../me looks around feeling snug
jsalonena at 2007-7-16 21:38:47 > top of Java-index,Java Essentials,Java Programming...