+ 1

How to round this to two significant decimal figure?

https://code.sololearn.com/ca111Fxb4BGe/?ref=app

18th Apr 2017, 11:24 AM
John Watson
John Watson - avatar
8 Antworten
+ 21
18th Apr 2017, 12:32 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 20
In addition to T0nd3's answer, you can also return the result as string using String.format(). Or return as double and then print using printf() method. Both the ways have been shown here : [EDIT]: https://code.sololearn.com/cwqSch5c8Sia/?ref=app
18th Apr 2017, 4:18 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
you can use: double pi = Math.PI; DecimalFormat decimalFormat = new DecimalFormat(",##0.00"); System.err.println(pi); System.err.println(decimalFormat.format(pi)); by adding 0 behind the "." you get more digits
18th Apr 2017, 11:46 AM
T0nd3
+ 2
or you can just convert double to float to keep the code simple :0 https://code.sololearn.com/cdzXp900UyIL/?ref=app
18th Apr 2017, 11:51 AM
Manideep
Manideep - avatar
+ 2
OK here you go... simple just add these two line to your code //float b=(float)a; String s = String.format("%.2f", b); https://code.sololearn.com/cdzXp900UyIL/?ref=app
18th Apr 2017, 12:36 PM
Manideep
Manideep - avatar
+ 1
shamima and shikamaru why is your code not working is there any import function to include?
18th Apr 2017, 12:19 PM
John Watson
John Watson - avatar
+ 1
try again john it works fine.. and I just added this one line to your code //float b=(float)a;
18th Apr 2017, 12:21 PM
Manideep
Manideep - avatar
+ 1
OK Shikamaru but this ain't 2 significant decimal
18th Apr 2017, 12:31 PM
John Watson
John Watson - avatar