# Thread: getting weird answer when multipling

1. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## 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

2. ## Re: getting weird answer when multipling

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

3. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## 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

4. ## Re: getting weird answer when multipling

Your posted code does not have a print statement. Where do you see the String that you posted?
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.

5. ## 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
Last edited by sunde887; 12-10-2011 at 08:18 PM.

6. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: getting weird answer when multipling

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

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);
JLabel label7 = new JLabel ("N/A");
}
else if (tempDays < 0){
String outDays = Integer.toString(tempDays*-1);
JLabel label6 = new JLabel ("Over due by " + outDays + " day(s)");
String lateFee = Double.toString((tempDays*-1)*.1);
JLabel label7 = new JLabel ("\$ " + lateFee);
}
}
would the easiest thing to do be subtract the 0.0000000000000001 or is there a ronding class?

See post # 5

8. ## 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.

9. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: getting weird answer when multipling

sweet it works now thanks for the help

10. Member
Join Date
Dec 2011
Posts
3
Rep Power
0

## 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".

11. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: getting weird answer when multipling

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.
Last edited by pbrockway2; 12-10-2011 at 11:34 PM.

13. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•