You can use the printf method, like so:
System.out.printf("%.2f", val);
In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).
There are other conversion characters you can use besides f:
d: decimal integero: octal integere: floating-point in scientific notation
You can use the printf method, like so:
System.out.printf("%.2f", val);
In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).
There are other conversion characters you can use besides f:
d: decimal integero: octal integere: floating-point in scientific notation
You can use DecimalFormat. One way to use it:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
Another one is to construct it using the #.## format.
I find all formatting options less readable than calling the formatting methods, but that's a matter of preference.
Rounding two decimal places using Printf
Using printf to round 49.765 to 49.77? - Unix & Linux Stack Exchange
What does formatted output mean in java? In what situations is printf used?
Print exact value of double
Videos
Hey guys, so I was just about to finish task one of my programming project when I realized that I needed to stop the ending number at 2 decimal places.
My professor does not wanting us using methods we have not learned yet, and he said that we could use PrintF, however we did not have to use PrintF.
Here is my code that is currently operational:
import java.util.Scanner;
public class A1 {
public static void main(String[] args)
{
int ValueOne;
int ValueTwo;
int ValueThree;
float Average;
Scanner in = new Scanner(System.in);
System.out.println("Enter first value: ");
ValueOne = in.nextInt();
System.out.println("You have entered int: "+ValueOne);
System.out.println("Enter second value: ");
ValueTwo = in.nextInt();
System.out.println("You have entered int: "+ValueTwo);
System.out.println("Enter third value: ");
ValueThree = in.nextInt();
System.out.println("You have entered int: "+ValueThree);
System.out.printf("The average of your three value is : " + ((ValueOne + ValueTwo + ValueThree) /3));
}}
I tried researching it before I came here, however did not find anything using PrintF. Any help is greatly appreciated thanks.
$ x=49.765
$ printf "%.2f" $(echo "$x + 0.005" | bc)
You have to use external commands because there is no built-in rounding feature in printf(1), and the POSIX shell doesn't have built-in floating-point arithmetic.
To round to the nearest decimal digit, you add 0.5 and truncate. To round to the nearest tenth, you divide the "nudge factor" by 10, and so forth.
This lack of built-in facilities is what often pushes people to use something like Perl rather than shell:
$ perl -e 'printf "%.2f", 49.765 + 0.005'
Same thing, but all handled by a single process.
You can use following command for rounding off.
float number = 49.765; printf("%0.2f", number);
You should be able to get the 2 figures after decimal point.
But this will just print, it will not update the value. If you would like to change the value of the variable then you should use below.
#include <math.h>
float val = 49.765;
float rounded_down = floorf(val * 100) / 100; /* Result: 49.76 */
float nearest = roundf(val * 100) / 100; /* Result: 49.77 */
float rounded_up = ceilf(val * 100) / 100; /* Result: 49.77 */
Notice that there are three different rounding rules you might want to choose: round down (ie, truncate after two decimal places), rounded to nearest, and round up. Usually, you want round to nearest.
As several others have pointed out, due to the quirks of floating point representation, these rounded values may not be exactly the "obvious" decimal values, but they will be very very close.