This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a NumberFormatter set up with Maximum and Minimum Fraction Digits set to 2.
Everythings worked well until my latest test where for some reason when I print the output to the screen my double (after being formatted) appears as 0.00 when it should really be 9.094947017729282E-13 this also happens when the number is negative -9.094947017729282E-13
Does anyone know why this is? I've tried altering the fractional digits but this doesn't solve it and I don't want any more than two digits following the decimal point.
A common way to compare doubles is to use a delta value (as you see in JUnit). You can implement this same logic with something like this:
I take it from your code above, that you actually have three conditions: zero, negative, and positive. You may want to use something like the logic I gave above to test for "equality" with zero first and then test for positive and negative values.
Only problem with that code is I don't know what the value is going to be, the method exists to determine whether the value is positive/negative and act accordingly.
I've tried understanding the purpose of Math.abs but failed miserably.
I would have thought it would be easy to compare a double (with a fractional precision of 2) with zero?
Can anyone offer any advice? I'm not particuarly strong with maths and can't think of any formula which I could use
Joined: Nov 11, 2004
I should add that the reason I thought I could use the following expression is res represents a currency figure and therefore anything less than a penny or 0.01 really isn't worth worrying about for this program.
So would this code be suitable or do you have a more suitable alternative? [ December 09, 2004: Message edited by: David Dickinson ]