Friday, 15 June 2012

java - Why System.out.printf will only work with %f when all of the variables are declared as double? -



java - Why System.out.printf will only work with %f when all of the variables are declared as double? -

here program. little 1 end of chapter exercises. problem when run it, works when have %f in system.printf when in mind should %d. of variables double , math.ceil returns double super confused. clarify please?

import java.util.scanner; public class excersise { public static void main(string[] args) { scanner input = new scanner(system.in); double enteredhours = 0; double amountdue = 0; system.out.printf("please come in number of hours parked."); enteredhours = input.nextdouble(); amountdue = calculatecharges(enteredhours); system.out.printf("the amount due %f\n", amountdue); // here line // when // changed // %f works, // 1 time again // variables // double , // math.ceil // returns // double. } public static double calculatecharges(double hours) { double roundedhours = 0; roundedhours = math.ceil(hours); double charges = 2.00; if (roundedhours > 3.0) charges = charges + .5 * roundedhours; if (roundedhours >= 24.0) charges = 10.00; homecoming charges; } }

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

%d format 'integer' %f formatting floats/doubles. think you're mistaking '%d' stand double

java

No comments:

Post a Comment