io - Dislaying an output decimal with 2 places in java -
i'm trying find out why %.2f declaration when outputting decimal isn't working in code, i've check other questions similar cant seem locate issue in specific logic error i'm receiving. when go compile program compiles fine, go run , outputs fine till final cost i'm trying display decimal value 2 decimal places.
i exception in thread "main"
java.util.illegalformatconversionexception f! = java.lang.string @ java.util.formatter$formatspecifier.failconversion(unknown source) @ java.util.formatter$formatspecifier.printfloat(unknown source) @ java.util.formatter.format(unknown source) @ java.io.printstream.format(unknown source) @ java.io.printstream.printf(unknown source) @ cars.main(cars.java:27)
here code:
import java.util.scanner; public class cars {
public static void main(string [] args) { scanner input = new scanner(system.in); int caryear, currentyear, carage; double costofcar, salestaxrate; double totalcost; string carmodel; system.out.println("please enter favorite car model."); carmodel = input.nextline(); system.out.println("please enter year of car"); caryear = input.nextint(); system.out.println("please enter current year."); currentyear = input.nextint(); carage = currentyear - caryear; system.out.println("how car cost?"); costofcar = input.nextdouble(); system.out.println("what sales tax rate?"); salestaxrate = input.nextdouble(); totalcost = (costofcar + (costofcar * salestaxrate)); system.out.printf("the model of favorite car is" + carmodel + ", car is" + " " + carage + " " + " years old, total of car is" + " " + "%.2f",totalcost + " " + " dollars."); }
}
like said before i'm not what's causing issue hints great on i'm overlooking.
try:
system.out.printf("the model of favorite car %s, car %d years old, total of car %.2f dollars.", carmodel, carage, totalcost);
or more readable:
system.out.printf("the model of favorite car %s," + " car %d years old," + " total of car %.2f dollars.", carmodel, carage, totalcost);
Comments
Post a Comment