1、金额格式化输出
val df = DecimalFormat("#0.00");
fun Double.format2(): String {
return df.format(this)//.removeSuffix(".00").removeSuffix(".0")
}
fun Double.formatAmt(): String {
if (this == 0.0) return "0"
val amtLong = (this.multiply(100)).toInt()
if (amtLong % 100 == 0) return this.toInt().toString()
return format2().dropLastWhile { it == '0' }.dropLastWhile { it == '.' }
}
2、金额计算
//double
fun Double.add(v1: Double): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal.valueOf(v1);
return b1.add(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.sub(v1: Double): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal.valueOf(v1);
return b1.subtract(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.multiply(v1: Double): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal.valueOf(v1);
return b1.multiply(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.divide(v1: Double): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal.valueOf(v1);
return b1.divide(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
//int
fun Double.add(v1: Int): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(v1.toString());
return b1.add(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.sub(v1: Int): Double {
val b1 = BigDecimal.valueOf(this)
val b2 = BigDecimal(v1.toString());
return b1.subtract(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.multiply(int: Int): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(int.toString());
return b1.multiply(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.divide(int: Int): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(int.toString());
return b1.divide(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
//long
fun Double.add(v1: Long): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(v1.toString());
return b1.add(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.sub(v1: Long): Double {
val b1 = BigDecimal.valueOf(this)
val b2 = BigDecimal(v1.toString());
return b1.subtract(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.multiply(int: Long): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(int.toString());
return b1.multiply(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}
fun Double.divide(int: Long): Double {
val b1 = BigDecimal.valueOf(this);
val b2 = BigDecimal(int.toString());
return b1.divide(b2).setScale(2, RoundingMode.HALF_UP).toDouble();
}