Last active
December 19, 2015 17:09
-
-
Save ironpeace/5989113 to your computer and use it in GitHub Desktop.
Javaで四捨五入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ironpeace; | |
import java.math.BigDecimal; | |
public class Sample { | |
public static void main(String[] args) { | |
System.out.println("1.234 -> " + round(1.234, 2)); // 1.23 | |
System.out.println("1.235 -> " + round(1.235, 2)); // 1.24 | |
System.out.println("1.236 -> " + round(1.236, 2)); // 1.24 | |
} | |
public static double round(double val, int digit){ | |
BigDecimal bd = new BigDecimal(val); | |
return bd.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment