Languages
[Edit]
EN

Java - round double to 2 decimal places

5 points
Created by:
Broncono
316

Quick solution:

public class RoundExample {

    public static double roundTo2thDecimalPlace(double value) {
        return Math.round(value * 100.0) / 100.0;
    }

    public static void main(String[] args) {
        System.out.println(roundTo2thDecimalPlace(1.23400000001)); // 1.23
        System.out.println(roundTo2thDecimalPlace(1.234)); // 1.23
        System.out.println(roundTo2thDecimalPlace(1.2)); // 1.2
    }
}

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join