mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
03f0d4d905
...
622aacaf44
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
622aacaf44 | ||
|
|
e78ac65605 |
@@ -466,14 +466,14 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="T运行cRound">T运行cRound</span>
|
||||
### <span id="TruncRound">TruncRound</span>
|
||||
|
||||
<p>截短n位小数(不进行四舍五入)</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```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>
|
||||
@@ -487,9 +487,9 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := mathutil.T运行cRound(0.124, 2)
|
||||
result2 := mathutil.T运行cRound(0.125, 2)
|
||||
result3 := mathutil.T运行cRound(0.125, 3)
|
||||
result1 := mathutil.TruncRound(0.124, 2)
|
||||
result2 := mathutil.TruncRound(0.125, 2)
|
||||
result3 := mathutil.TruncRound(0.125, 3)
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
@@ -396,7 +396,10 @@ func RemoveNonPrintable(str string) string {
|
||||
// StringToBytes converts a string to byte slice without a memory allocation.
|
||||
// Play: https://go.dev/play/p/7OyFBrf9AxA
|
||||
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.
|
||||
|
||||
@@ -518,7 +518,8 @@ func TestStringToBytes(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStringToBytes")
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user