getting weird answer when multipling

when im multilpying using this code

a*.1

im keep get this answer

0.a000000000000001

i dont understand where its coming from, all i want is .a without all the other stuff, what is the easiest way of doing this and why is it happening?

a is an int, and im using the newest versoin of netbeans

any help is greatly apperciated

Re: getting weird answer when multipling

Please post the full text of the statement(s) you are using that creates that output.

Re: getting weird answer when multipling

int tempDays = 30 - days[i];

else if (tempDays < 0){

String lateFee = Double.toString((tempDays*-1)*.1);

JLabel label7 = new JLabel ("$ " + lateFee);

this enough? its for a gui window

Re: getting weird answer when multipling

Your posted code does not have a print statement. Where do you see the String that you posted?

Quote:

0.a000000000000001

Show the code that builds that String. Add a debug println() statement to print it out so we can see what yor code is doing.

Re: getting weird answer when multipling

It's just the nature of how multiplication works on computer. Try using BigDecimal (I think that it). http://docs.oracle.com/javase/6/docs...malFormat.html

1 Attachment(s)

Re: getting weird answer when multipling

sorry for being confusing, this is the window that is printed

Attachment 2249

and i want it to display .7 instead of .700000000000001

this is the whole for loop that figures out how much each book is over due by if at all

for (int i=0; i <= (books.length-1); i++){

int tempDays = 30 - days[i];

if (tempDays >= 0){

String outDays = Integer.toString(tempDays);

JLabel label6 = new JLabel (outDays);

box1.add(label6);

box1.add(Box.createVerticalStrut(5));

JLabel label7 = new JLabel ("N/A");

box2.add(label7);

box2.add(Box.createVerticalStrut(5));

}

else if (tempDays < 0){

String outDays = Integer.toString(tempDays*-1);

JLabel label6 = new JLabel ("Over due by " + outDays + " day(s)");

box1.add(label6);

box1.add(Box.createVerticalStrut(5));

String lateFee = Double.toString((tempDays*-1)*.1);

JLabel label7 = new JLabel ("$ " + lateFee);

box2.add(label7);

box2.add(Box.createVerticalStrut(5));

}

}

would the easiest thing to do be subtract the 0.0000000000000001 or is there a ronding class?

Re: getting weird answer when multipling

Re: getting weird answer when multipling

There are number format classes that will allow you to specify the number of decimal places.

Also the String class has a format method.

Re: getting weird answer when multipling

sweet it works now thanks for the help :D:

Re: getting weird answer when multipling

That "1" at the end doesn't make any sense as repeating decimals would make all the digits after 10 digits the same as the first 10, which is "0".

Re: getting weird answer when multipling

Quote:

That "1" at the end doesn't make any sense

On the contrary the Java Language Specification in section 4.2.3 Floating-Point Types, Formats, and Values precisely gives the sense: "The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and **operations as specified in ***IEEE Standard for Binary Floating-Point Arithmetic*, ANSI/IEEE Standard 754-1985 (IEEE, New York)."

Not that I've ever read IEEE 754-1985, but the general principle is clear enough. The language only deals with floating point quantities using a finite amount of precision: there are, after all, infinitely many such quantities so that representing them all uniquely would require an infinite amount of memory/paper/screen space/time etc. The point is that, there being an infinite number of them, the symbols or bit patterns in memory for at least some of them would have to be arbitrarily large. So the language (and the computer) is obliged to work with imprecise surrogates which are "near enough".

(Integer values suffer from the same problem, but here the computer offers complete precision but at a cost of ignoring all the integer values past a certain point. Even with limited precision the floating point values must also be limited. The bottom line is you can only specify a finite number of different numbers.)

Floating point numbers are, in a sense, "fuzzy". You get a certain number of decimal places, and that's it. Where an actual real world quantity is being represented this way there are no grounds for thinking that the digits you don't see are zero, or seven, or anything else. They are simply not represented.

Although the relationship between Java floating point types (and those other languages) and real world quantities is that of "imprecise resemblance", the operations on them do have to be defined precisely (otherwise nobody would know what is going on). That is where the IEEE standard comes in and, as far as I know, Java adheres to it exactly.

Of course the IEEE folk may have cooked up a crazy standard! Their cleverness and the usefulness of the standard over time is only so much evidence in their favour. But before suggesting a better or more straight forward alternative it might pay to read the standard and consider the issues that it addresses.

Re: getting weird answer when multipling

Re: getting weird answer when multipling

The representation of 0.1

is not a recurring fraction in base 10.

It is a recurring fraction in binary -- namely 0-000110011'0011'

- so you have rounding error, because the CPU works in binary, not in the decimal base in which you have written the number.