Computer Science, asked by ashamishra170pci228, 1 year ago

how to type two decimal places in java? ex- INPUT - 12.367 OUTPUT - 12.37 WITHOUT USING MATH CLASS

Answers

Answered by siddhartharao77
10

Method - 1:

import java.util.*;

class Demo

{

public static void main(String[] args)

{

double d = 12.367;

System.out.printf("%.2f", d);

}

}

-----------------------------------------------------------------------------------------------------------------

Method - 2:

import java.text.*;

class Demo

{

public static void main(String[] args)

{

double d = 12.367;

DecimalFormat hello = new DecimalFormat("#.##");

System.out.println(hello.format(d));

}

}

----------------------------------------------------------------------------------------------------------------

Output: 12.37.




Hope this helps!


ashamishra170pci228: thank you
siddhartharao77: ok
ashamishra170pci228: if i want to use math class then????
siddhartharao77: its very simple...use direct math methods.. math.round or floor
ashamishra170pci228: i tried but the answer are not exact 12.36
ashamishra170pci228: sry 12.37
siddhartharao77: U will get the correct answer.... post the question after 5 minutes if possible..i will solve
ashamishra170pci228: oky
Similar questions