How to round a user input in Java -


i'm attempting user input of decimal, round 2 decimal points. code have not working correctly, , i'm not sure why.

package code; import java.math.roundingmode; import java.text.decimalformat; import java.util.scanner; public class decimalplaces {      public static void main(string[] args) {         decimalformat df = new decimalformat("#.##");         df.setroundingmode(roundingmode.ceiling);         scanner qweinput = new scanner(system.in);         system.out.println("enter decimal number:");         string qwe1 = qweinput.next();         df.format(qwe1);         system.out.println(qwe1);         }  } 

with scanner, better use nextline() when can it, preserves errors return line char:

string qwe1 = qweinput.nextline(); 

then need parse double, because if not tries cast object double , crashes

df.format(double.parsedouble(qwe1)); 

then format method return string formated, because string immutable, need print direclty or save :

qwe1 = df.format(double.parsedouble(qwe1)); system.out.println(qwe1); //----------------------------------or---------------------------------- system.out.println(df.format(double.parsedouble(qwe1))); 

edit : avoid parsing double can use nextdouble() scanner, it direclty save double, save format need string so, proper name ;)


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -