Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Point with 2 Digits after Point

I will Format a Floating Point Number in android with 2 Digits after the point. I search for a Example but i din´t find one.

Can some one help me.

like image 384
Happo Avatar asked Dec 08 '10 19:12

Happo


People also ask

How do you print 2 digits after a decimal point?

In Python, to print 2 decimal places we will use str. format() with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.

Can a float have two decimals?

The float data type has only 6-7 decimal digits of precision.

How do I show 2 digits after a decimal in Python?

Use str. format() with “{:. 2f}” as string and float as a number to display 2 decimal places in Python. Call print and it will display the float with 2 decimal places in the console.

How do you limit a float to two decimal places?

We will use %. 2f to limit a given floating-point number to two decimal places.


2 Answers

float f = 2.3455f;   String test = String.format("%.02f", f); 
like image 175
Ryan Reeves Avatar answered Nov 07 '22 09:11

Ryan Reeves


You can also use DecimalFormat

float f = 102.236569f;  DecimalFormat decimalFormat = new DecimalFormat("#.##"); float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24 //double twoDigitsF = Double.valueOf(decimalFormat.format(f)); also format double value     
like image 26
Linh Avatar answered Nov 07 '22 09:11

Linh