java - Printing first 3 decimal places without rounding -


okay, know there question quite similar already, didn't quite answer question. please let me know if i'm doing wrong though.

i have written program gets temperature in fahrenheit user , converts celcius. looks this:

import java.util.scanner;  public class fahrenheittocelcius {      public static void main(string[] args) {          double fahrenheit;         scanner sc = new scanner(system.in);          system.out.print("please enter temperature in fahrenheit: ");          fahrenheit = sc.nextdouble();           double celcius = (fahrenheit - 32) * 5 / 9;          string num = string.format("%.3f", celcius);          system.out.println(" ");         system.out.println(fahrenheit + "f" + " " + num + "c");     }  } 

when prints answer out, want first 3 decimal places printed, not rounded. example, if input 100f, want 37.777c printed, not 37.778c.

i have tried using decimalformat , string.format (like above), both methods print out 37.778c. there better way this?

thank answers, , apologise if duplicate.

you can use decimalformat, set roundingmode:

decimalformat df = new decimalformat("#.###"); df.setroundingmode(roundingmode.floor); string num = df.format(celcius); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -