Compare commits

..
2 Commits
3 changed files with 11 additions and 7 deletions
+5 -5
View File
@@ -466,14 +466,14 @@ func main() {
} }
``` ```
### <span id="T运行cRound">T运行cRound</span> ### <span id="TruncRound">TruncRound</span>
<p>截短n位小数(不进行四舍五入)</p> <p>截短n位小数(不进行四舍五入)</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func T运行cRound[T constraints.Float | constraints.Integer](x T, n int) T func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T
``` ```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/aumarSHIGzP)</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/aumarSHIGzP)</span></b>
@@ -487,9 +487,9 @@ import (
) )
func main() { func main() {
result1 := mathutil.T运行cRound(0.124, 2) result1 := mathutil.TruncRound(0.124, 2)
result2 := mathutil.T运行cRound(0.125, 2) result2 := mathutil.TruncRound(0.125, 2)
result3 := mathutil.T运行cRound(0.125, 3) result3 := mathutil.TruncRound(0.125, 3)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
+4 -1
View File
@@ -396,7 +396,10 @@ func RemoveNonPrintable(str string) string {
// StringToBytes converts a string to byte slice without a memory allocation. // StringToBytes converts a string to byte slice without a memory allocation.
// Play: https://go.dev/play/p/7OyFBrf9AxA // Play: https://go.dev/play/p/7OyFBrf9AxA
func StringToBytes(str string) (b []byte) { func StringToBytes(str string) (b []byte) {
return *(*[]byte)(unsafe.Pointer(&str)) return *(*[]byte)(unsafe.Pointer(&struct {
string
Cap int
}{str, len(str)}))
} }
// BytesToString converts a byte slice to string without a memory allocation. // BytesToString converts a byte slice to string without a memory allocation.
+2 -1
View File
@@ -518,7 +518,8 @@ func TestStringToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestStringToBytes") assert := internal.NewAssert(t, "TestStringToBytes")
bytes := StringToBytes("abc") bytes := StringToBytes("abc")
assert.Equal(bytes, []byte{'a', 'b', 'c'}) assert.Equal([]byte{'a', 'b', 'c'}, bytes)
assert.Equal(3, cap(bytes))
} }
func TestBytesToString(t *testing.T) { func TestBytesToString(t *testing.T) {