Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jani
Last active December 8, 2015 10:52
Show Gist options
  • Select an option

  • Save vaibhav-jani/fd4df4de44a01378f338 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhav-jani/fd4df4de44a01378f338 to your computer and use it in GitHub Desktop.
Round the decimal with coefficient
// amount = 1.2244 with round 0.003 will be 1.23, with round 0.005 will be 1.22, with round 0.03 will be 1.3
public static String taxFormat(double taxAmount, double round) {
try {
String extensionRemoved = String.valueOf(round).split("\\.")[1];
int sigCount = extensionRemoved.length() - 1;
double addjust = 1.0;
for (int i = 0; i < sigCount; i++) {
addjust = addjust/10.0;
}
double coefficient = addjust - round;
taxAmount = taxAmount + coefficient;
BigDecimal bigDecimal = new BigDecimal(taxAmount);
bigDecimal = bigDecimal.setScale(sigCount, BigDecimal.ROUND_DOWN);
//bigDecimal.doubleValue();
String value = String.valueOf(bigDecimal);
return value;
} catch (Exception e) {
e.printStackTrace();
}
return String.valueOf(taxAmount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment