我在 golang 中使用github.com/shopspring/decimal進行十進制相關操作,而不是使用 float 來獲得實際精度。我需要找到十進制庫值的十進制計數。
例如:-
price := decimal.NewFromFloat(0.00355)
fmt.Println("Price:", price) // Price:0.00355
這就是我使用小數的方式。所以現在我想計算同一個小數的小數位數。在上面的示例中,小數計數將是5
因為有五個小數點。
檢查了檔案,但找不到正確的操作。有人可以幫忙嗎?提前致謝。
uj5u.com熱心網友回復:
您可以將十進制值轉換為字串,然后計算 point 之后的字符數.
。
0.00355 => "0.00355" => count=5
uj5u.com熱心網友回復:
你不會直接使用decimal.Exponent()
并詢問它的規模是多少嗎?
https://goplay.tools/snippet/9Y6BP1dcOuA
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
x := decimal.NewFromFloat(123.456789)
s := x.Exponent()
fmt.Printf("decimal value %v has a scale of %v\n", x, s)
}
哪個產量
decimal value 123.456789 has a scale of -6
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516949.html
標籤:去
上一篇:從GRPC獲取請求詳細資訊