Converting the float value into a two decimal points in Java

import java.io.*;
import java.lang.*;
public class hello
{
public static void main(String args[])
{
float a=1222.222f,b;
b=Math.round(a*100)/100.0f;
System.out.println(b);

}
}

 

O/P:  1222.22

*Note dont forget to divide by 100.0f

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s