Skip to content

Instantly share code, notes, and snippets.

@ironpeace
Last active December 19, 2015 17:09
Show Gist options
  • Save ironpeace/5989113 to your computer and use it in GitHub Desktop.
Save ironpeace/5989113 to your computer and use it in GitHub Desktop.
Javaで四捨五入
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