Help with double arithmetic

H

harry

I have a double array & 2 double var's -

double subTotals[10][10];
double x = 9670000.0;
double y = 180000.0;

Doing something like -

subTotals[1][1] = x + y;

gives subTotals[1][1] = 9850000.0 which is fine

but when

double x = 9850000.0 ;
double y = 270000.0;

subTotals[1][1] = 1.012E7

I need to extract this value (which is displayed on a web page) but the
value 1.012E7 comes out - I guess this is due to the exponent & mantissa of
floating point numbers - what not really sure how to handle this!

Do I when using the value have to pass the value through some function to
retrieve the full value i.e 10120000 or what?

many thanks

harry
 
B

Betty

harry said:
I have a double array & 2 double var's -

double subTotals[10][10];
double x = 9670000.0;
double y = 180000.0;

Doing something like -

subTotals[1][1] = x + y;

gives subTotals[1][1] = 9850000.0 which is fine

but when

double x = 9850000.0 ;
double y = 270000.0;

subTotals[1][1] = 1.012E7

I need to extract this value (which is displayed on a web page) but the
value 1.012E7 comes out - I guess this is due to the exponent & mantissa of
floating point numbers - what not really sure how to handle this!

Do I when using the value have to pass the value through some function to
retrieve the full value i.e 10120000 or what?

many thanks

harry

// this gives 4 digit, you can change it to more if you want
import java.text.DecimalFormat;
double a, x;
static final DecimalFormat f = new DecimalFormat("0.####");
a = f.format(x).toString(); // four digit rounding
 
T

Thomas G. Marshall

Betty coughed up:
harry said:
I have a double array & 2 double var's -

double subTotals[10][10];
double x = 9670000.0;
double y = 180000.0;

Doing something like -

subTotals[1][1] = x + y;

gives subTotals[1][1] = 9850000.0 which is fine

but when

double x = 9850000.0 ;
double y = 270000.0;

subTotals[1][1] = 1.012E7

I need to extract this value (which is displayed on a web page) but
the value 1.012E7 comes out - I guess this is due to the exponent &
mantissa of floating point numbers - what not really sure how to
handle this!

Do I when using the value have to pass the value through some
function to retrieve the full value i.e 10120000 or what?

many thanks

harry

// this gives 4 digit, you can change it to more if you want
import java.text.DecimalFormat;
double a, x;
static final DecimalFormat f = new DecimalFormat("0.####");
a = f.format(x).toString(); // four digit rounding

(?)

I don't think you understood the OP's question. He doesn't want extra
digits behind the decimal point, he wants none at all.

Secondly, your last line calls format(), converts the StringBuffer to a
String and then assigns it to a double.

This will produce the number 10,120,000 (this is just to show the OP what NF
can do):

NumberFormat nf = NumberFormat.getInstance();

double x = 9850000.0;
double y = 270000.0;

System.out.println(nf.format(x+y));

If he wants the number printed out as 10120000 instead, he merely has to do
this:

NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false); // <----note----

double x = 9850000.0;
double y = 270000.0;

System.out.println(nf.format(x+y));
 
B

Betty

Thomas G. Marshall said:
Betty coughed up:
harry said:
I have a double array & 2 double var's -

double subTotals[10][10];
double x = 9670000.0;
double y = 180000.0;

Doing something like -

subTotals[1][1] = x + y;

gives subTotals[1][1] = 9850000.0 which is fine

but when

double x = 9850000.0 ;
double y = 270000.0;

subTotals[1][1] = 1.012E7

I need to extract this value (which is displayed on a web page) but
the value 1.012E7 comes out - I guess this is due to the exponent &
mantissa of floating point numbers - what not really sure how to
handle this!

Do I when using the value have to pass the value through some
function to retrieve the full value i.e 10120000 or what?

many thanks

harry

// this gives 4 digit, you can change it to more if you want
import java.text.DecimalFormat;
double a, x;
static final DecimalFormat f = new DecimalFormat("0.####");
a = f.format(x).toString(); // four digit rounding

(?)

I don't think you understood the OP's question. He doesn't want extra
digits behind the decimal point, he wants none at all.

I wanted the OP to do some of the work.
Just wanted to get him started with format, expecting that he would look it
up.
 
?

.

I have a double array & 2 double var's -

double subTotals[10][10];
double x = 9670000.0;
double y = 180000.0;

Doing something like -

subTotals[1][1] = x + y;

gives subTotals[1][1] = 9850000.0 which is fine

but when

double x = 9850000.0 ;
double y = 270000.0;

subTotals[1][1] = 1.012E7

I need to extract this value (which is displayed on a web page) but the
value 1.012E7 comes out - I guess this is due to the exponent & mantissa of
floating point numbers - what not really sure how to handle this!

Do I when using the value have to pass the value through some function to
retrieve the full value i.e 10120000 or what?

Someone has shown you the solution but I thought I'd take a moment to show
you how you could have found the solution.

The number 1.012E7 and 10120000.0 are the same thing. The value you see on
the output is dependent on how you choose to display it. If you use:

System.out.println(subTotals[1][1]);

read up on the println() method in the Java API documentation, the it
actually translate to:

System.out.print(subTotals[1][1]);
System.out.println();

The call to print() will convert the double to String using:

String.valueOf(subTotals[1][1]);

The documentation on String.valueOf() indicates it is the same as:

Double.toString(subTotal[1][1]);

Here the documentation tells me exactly what to expect. If the magnitude
is less than 10^7 then you will get the output you want. If the magnitude
is equal to or greater than 10^7 you will get the computerized scientific
notiation, e.g. 1.012E7.

It goes on to say that I can use subclasses of java.text.NumberFormat to
change the output. In other words, rather than letting println() convert
the double to a String then print it, I can convert it to a String and
pass the String to println(). The documentation for NumberFormat indicates
that DecimalFormat is a known subclass.

So I look up the documentation on java.text.DecimalFormat and figure out
how to use it to convert the double to a String in the format I desire
then pass the String to println().

Here is some sample code:

import java.text.DecimalFormat;

class FormattingDoubles {
public static void main(String[] arg) {
double x = 9850000.0;
double y = 270000.0;
DecimalFormat f = new DecimalFormat("0.#");
String tmp = f.format((x+y)).toString();
System.out.println(tmp);
}
}

The "0.#" determines how the number will print. Look up the details on the
DecimalFormat documentation if you want to format it differently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top