Can i use a double and restrict the number after the decimal? See example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can i use a double and restrict the number after the decimal? See example

// the mins go over 59 i want to stop that if possible i know i could do each one individually but wonding if theres a faster way// Scanner scn = new Scanner(System.in); double time = scn.nextDouble(); if(time < 12.00 && time > -1 ){System.out.println(time + " A.M");} else if(time < 24.00 && time > 11.59){System.out.println(time + " PM");} else{System.out.println("Time Unknown");}

16th Aug 2017, 9:26 PM
D_Stark
D_Stark - avatar
2 Answers
0
use decimal formatter.
16th Aug 2017, 10:53 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
Yes you can. Instead of using println, you have to use printf, where the f means format. For 2 decimal places, printf("%f.2",time). So if you want three decimal places, make it .3,etc.
1st Sep 2017, 11:15 AM
Amos Aidoo
Amos Aidoo - avatar