PDA

View Full Version : How to format integer data


DaichiAzure
09-04-2008, 01:50 PM
I need to format a double integer value to display only out to the hundredths place. What would I need to use to do so?

DragoNero
09-05-2008, 02:28 AM
Have you got an example?

DaichiAzure
09-05-2008, 07:28 AM
import java.util.*;

public class changeCounter{



public static void main(String[] args) {

int quarter;
int dimes;
int nickles;
int pennies;
double sum = 0;

Scanner mst3k = new Scanner(System.in);

System.out.println("How many quarters do you have in yer pocket?");

quarter = mst3k.nextInt(); //scans for quarters

sum = quarter * .25;

System.out.println("How many dimes do you have in yer pocket?");
dimes = mst3k.nextInt(); //scans for dimes
sum = sum + (dimes * .10);

System.out.println("How many nickles do you have in yet pocket?");
nickles = mst3k.nextInt();
sum = sum + (nickles * .05); // scans for nickles

System.out.println("How many pennies do you have in yer pocket?");
pennies = mst3k.nextInt(); // scans for pennies...why we still use them is beyond me
sum = sum + (pennies * .01);

System.out.println("Here is your change Sir/Ma'am. It totals $" + sum);
} //end main
} // end class

when I get the total it has the right value but it winds up as $XX.XX000000000000002 as such

i'm trying to get "new PrintfFormat("\`%10.2f\'").sprintf(1.0);" to limit the number given down to 2 decimal places

DragoNero
09-07-2008, 04:41 PM
try using double or float instead of int

nandy
10-25-2008, 06:07 PM
Try this one...


import java.util.*;
import java.text.*;
public class changeCounter{



public static void main(String[] args) {

int quarter;
int dimes;
int nickles;
int pennies;
double sum = 0;
DecimalFormat df = new DecimalFormat("0.00");
Scanner mst3k = new Scanner(System.in);

System.out.println("How many quarters do you

have in yer pocket?");

quarter = mst3k.nextInt(); //scans for

quarters

sum = quarter * .25;

System.out.println("How many dimes do

you have in yer pocket?");
dimes = mst3k.nextInt();

//scans for dimes
sum = sum + (dimes * .10);

System.out.println("How many nickles do

you have in yet pocket?");
nickles = mst3k.nextInt();
sum = sum + (nickles * .05); //

scans for nickles

System.out.println("How many pennies do

you have in yer pocket?");
pennies = mst3k.nextInt(); //

scans for pennies...why we still use them is beyond me
sum = sum + (pennies * .01);

System.out.println("Here is your change

Sir/Ma'am. It totals $" + df.format(sum));
} //end main
} // end class