mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	优化数字格式化
This commit is contained in:
		@@ -87,28 +87,14 @@ func FormatFloat(f interface{}, decimal int) string {
 | 
			
		||||
		var dotIndex = strings.Index(s, ".")
 | 
			
		||||
		if dotIndex > 0 {
 | 
			
		||||
			var d = s[:dotIndex]
 | 
			
		||||
			var l = len(d)
 | 
			
		||||
			if l > 3 {
 | 
			
		||||
				var f2 = s[dotIndex:] // 包含点(.)
 | 
			
		||||
				var pieces = l / 3
 | 
			
		||||
				var commIndex = l - pieces*3
 | 
			
		||||
				var d2 = ""
 | 
			
		||||
				if commIndex > 0 {
 | 
			
		||||
					d2 = d[:commIndex] + ", "
 | 
			
		||||
				}
 | 
			
		||||
				for i := 0; i < pieces; i++ {
 | 
			
		||||
					d2 += d[commIndex + i*3 : commIndex + i*3+3]
 | 
			
		||||
					if i != pieces-1 {
 | 
			
		||||
						d2 += ", "
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				return d2 + f2
 | 
			
		||||
			}
 | 
			
		||||
			var f2 = s[dotIndex:]
 | 
			
		||||
			f2 = strings.TrimRight(strings.TrimRight(f2, "0"), ".")
 | 
			
		||||
			return formatDigit(d) + f2
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return s
 | 
			
		||||
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
 | 
			
		||||
		return types.String(x)
 | 
			
		||||
		return formatDigit(types.String(x))
 | 
			
		||||
	case string:
 | 
			
		||||
		return x
 | 
			
		||||
	}
 | 
			
		||||
@@ -129,3 +115,23 @@ func TrimZeroSuffix(s string) string {
 | 
			
		||||
	}
 | 
			
		||||
	return strings.TrimRight(strings.TrimRight(matches[1], "0"), ".") + matches[2]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func formatDigit(d string) string {
 | 
			
		||||
	var l = len(d)
 | 
			
		||||
	if l > 3 {
 | 
			
		||||
		var pieces = l / 3
 | 
			
		||||
		var commIndex = l - pieces*3
 | 
			
		||||
		var d2 = ""
 | 
			
		||||
		if commIndex > 0 {
 | 
			
		||||
			d2 = d[:commIndex] + ", "
 | 
			
		||||
		}
 | 
			
		||||
		for i := 0; i < pieces; i++ {
 | 
			
		||||
			d2 += d[commIndex+i*3 : commIndex+i*3+3]
 | 
			
		||||
			if i != pieces-1 {
 | 
			
		||||
				d2 += ", "
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return d2
 | 
			
		||||
	}
 | 
			
		||||
	return d
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -41,6 +41,10 @@ func TestFormatFloat(t *testing.T) {
 | 
			
		||||
	t.Log(numberutils.FormatFloat(12345678.012, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(123456789.012, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(1234567890.012, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(123, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(1234, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(1234.00001, 4))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(1234.56700, 4))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestTrimZeroSuffix(t *testing.T) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user