应该很容易。我最初会在javascript中执行此操作,但必须在设置到我的处理程序页面中的表单之前执行此操作。无论如何,我需要使这些值有2位小数。例如,219333.5888888需要219333.58。是否有修剪功能?
form.setUnitRepairCost(Double.toString(jobPlanBean.getUnitTotalCost())); //UNIT REPAIR COST
form.setUnitMaterialCost(Double.toString(jobPlanBean.getUnitTotalMaterialCost())); //UNIT MATERIAL COST
答案 0 :(得分:42)
这是格式化十进制值的简单示例
import java.text.*;
public class DecimalPlaces {
public static void main(String[] args) {
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
}
答案 1 :(得分:10)
public static void main(String[] args) {
double d = 6.3546;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
答案 2 :(得分:7)
获得双后卫而不是字符串:
double d = 80.123;
DecimalFormat df = new DecimalFormat("#.##");
double p = Double.parseDouble(df.format(d));
答案 3 :(得分:6)
将double乘以100.0并将其转换为int然后取出该int并将其转换为double并除以100.0
int temp = (int)(longDouble*100.0);
double shortDouble = ((double)temp)/100.0;
答案 4 :(得分:3)
怎么样:
new java.text.DecimalFormat("0.00").format( yourNumber );
答案 5 :(得分:2)
这是String操作,用于将double值截断到小数位数。
public static String truncateUptoTwoDecimal(double doubleValue) {
String value = String.valueOf(doubleValue);
if (value != null) {
String result = value;
int decimalIndex = result.indexOf(".");
if (decimalIndex != -1) {
String decimalString = result.substring(decimalIndex + 1);
if (decimalString.length() > 2) {
result = value.substring(0, decimalIndex + 3);
} else if (decimalString.length() == 1) {
result = String.format(Locale.ENGLISH, "%.2f",
Double.parseDouble(value));
}
}
return result;
}
return null;
}
答案 6 :(得分:2)
根据其他建议,您可以使用DecimalFormat
的类java.text.DecimalFormat
。我们还可以使用DecimalFormat
舍入十进制值。
示例:
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalDemo {
private static DecimalFormat decimalFormatter = new DecimalFormat("#.##");
public static void main(String[] args) {
double number = 2.14159265359;
System.out.println("Original Number : " + number);
System.out.println("Upto 2 decimal : " + decimalFormatter.format(number)); //2.14
// DecimalFormat, default is RoundingMode.HALF_EVEN
decimalFormatter.setRoundingMode(RoundingMode.DOWN);
System.out.println("Down : " + decimalFormatter.format(number)); //2.14
decimalFormatter.setRoundingMode(RoundingMode.UP);
System.out.println("Up : " + decimalFormatter.format(number)); //2.15
}
}
答案 7 :(得分:1)
是,DecimalFormat:http://www.exampledepot.com/egs/java.text/FormatNum.html
答案 8 :(得分:1)
使用十进制格式:
DecimalFormat twoDForm = new DecimalFormat("#.##");
答案 9 :(得分:1)
答案 10 :(得分:0)
public static double truncateDecimals(double d, int len) {
long p = pow(10, len);
long l = (long)(d * p);
return (double)l / (double)p;
}
public static long pow(long a, int b) {
long result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
}
return result;
}
答案 11 :(得分:0)
通过使用这两种方法,您还可以处理所有异常:
private String convertedBalance(String balance){
String convertedBalance = balance.toString();
Double d;
try {`enter code here`
d = Double.parseDouble(balance.toString());
Log.i("ConvertedNumber", "d (amount) = "+d.toString());
d = round(d, 2);
DecimalFormat f = new DecimalFormat("0.00");
convertedBalance = f.format(d);
Log.i("ConvertedNumber", "convertedBalance = "+convertedBalance);
}catch (NumberFormatException e){
Log.i("ConvertedNumber", "Number format exception");
}
return convertedBalance;
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}