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.

9.094947017729282E-13 to 2 decimal places is 0.00.

You are expecting NumberFormatter to give you scientific notation? I didn't know it could do that. Please show your code.

Mike Gershman
SCJP 1.4, SCWCD in process

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66

posted

0

Originally posted by Mike Gershman: You are expecting NumberFormatter to give you scientific notation? I didn't know it could do that. Please show your code.

I didn't expect the result in scientific notation, is this performed automatically by the double type when a value becomes to long? Do you know of any way to force it to store the numeric value?

The formatted number will be 0.00 when rounded to two decimal places if it's between -0.0049* and 0.0049* . The UNformatted number (so your double) doesn't change.

You can't have it both ways, either you get it printed rounded to two decimal places or you don't...

42

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66

posted

0

Well in my code I evaluate the double:

But because the actual value of the double is 0.000000000000001 (or something very similar) it falls into the second condition which I don't want it to.

Can you think of any way of stopping this? In my JUnit tests I just set the delta to 0.01 which effectively skips any minute differences.

Basically i'd be happier if the value was .01 different from 0 for it to fall into either condition.

So am I right in thinking the test simply needs changing to:

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

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66

posted

0

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 ]