Selaa lähdekoodia

float格式化方法

Jianghan 2 vuotta sitten
vanhempi
commit
d9e9752262
1 muutettua tiedostoa jossa 43 lisäystä ja 0 poistoa
  1. 43 0
      common.go

+ 43 - 0
common.go

@@ -721,3 +721,46 @@ func DeepCopy(value interface{}) interface{} {
 	}
 	return value
 }
+
+// 保留两位小数,舍弃尾数,无进位运算
+// 主要逻辑就是先乘,trunc之后再除回去,就达到了保留N位小数的效果
+func FormatFloat(num float64, decimal int) (float64, error) {
+	// 默认乘1
+	d := float64(1)
+	if decimal > 0 {
+		// 10的N次方
+		d = math.Pow10(decimal)
+	}
+	// math.trunc作用就是返回浮点数的整数部分
+	// 再除回去,小数点后无效的0也就不存在了
+	res := strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
+	return strconv.ParseFloat(res, 64)
+}
+
+// 舍弃的尾数不为0,强制进位
+func FormatFloatCeil(num float64, decimal int) (float64, error) {
+	// 默认乘1
+	d := float64(1)
+	if decimal > 0 {
+		// 10的N次方
+		d = math.Pow10(decimal)
+	}
+	// math.trunc作用就是返回浮点数的整数部分
+	// 再除回去,小数点后无效的0也就不存在了
+	res := strconv.FormatFloat(math.Ceil(num*d)/d, 'f', -1, 64)
+	return strconv.ParseFloat(res, 64)
+}
+
+// 强制舍弃尾数
+func FormatFloatFloor(num float64, decimal int) (float64, error) {
+	// 默认乘1
+	d := float64(1)
+	if decimal > 0 {
+		// 10的N次方
+		d = math.Pow10(decimal)
+	}
+	// math.trunc作用就是返回浮点数的整数部分
+	// 再除回去,小数点后无效的0也就不存在了
+	res := strconv.FormatFloat(math.Floor(num*d)/d, 'f', -1, 64)
+	return strconv.ParseFloat(res, 64)
+}