将 `decimal.NullDecimal` 类型转换为 int
Cast `decimal.NullDecimal` type to int
我正在使用类型为 decimal.NullDecimal
(来自 github.com/shopspring/decimal
)的变量,并希望转换其整数值以便能够将其与整数进行比较。我应该如何转换它的十进制值?
这是我的代码:
import (
// other packages
"github.com/shopspring/decimal"
)
type BooksStores struct {
PublisherShare decimal.NullDecimal `json:"publisher_share"`
}
var booksStores BooksStores
err := c.ShouldBindBodyWith(&booksStores, binding.JSON)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"error": err.Error(),
})
return
}
publisherShare := booksStores.PublisherShare
// Here I want to compare the `publisherShare` value with 0 and 100
if publisherShare < 0 || publisherShare > 100 {
// DO SOMETHING
}
这是我尝试过的方法,但 none 没有成功:
import "https://github.com/spf13/cast"
cast.ToInte(publisherShare)
cast.ToInte(&publisherShare)
cast.ToInt(publisherShare.Decimal)
cast.ToInt(publisherShare.Decimal)
// but it is casted to zero, I have to use `decimal.NullDecimal` type
// due to the database considerations (later I have to update something)
我认为我应该使用 github.com/shopspring/decimal
包中的 Cmp
方法来比较 publisherShare
值与数字。
由于并非所有任意精度的小数都可以表示为 Go 整数,我想说你应该使用 decimal.NewFromInt(...)
将你的 int 转换为小数,然后使用 decimal.GreaterThan 之类的东西进行比较:
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
// Let's say dec is the decimal we want to compare
dec := decimal.NewFromInt(42)
// We compare it to 100
h := decimal.NewFromInt(100)
if dec.GreaterThan(h) {
fmt.Println("> 100")
} else {
fmt.Println("<= 100")
}
}
请注意,decimal.NullDecimal
仅包含一个 Decimal
和一个有效性标志,因此一旦您知道它是有效的,您只需从中获取 Decimal
字段,然后如上所示进行比较。
好的,现在问题解决了,我应该用var booksStores *BooksStores
而不是var booksStores BooksStores
。
type BooksStores struct {
PublisherShare decimal.NullDecimal `json:"publisher_share"`
}
var booksStores *BooksStores // Here is the place I had to change
err := c.ShouldBindBodyWith(&booksStores, binding.JSON)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"error": err.Error(),
})
return
}
publisherShare := booksStores.PublisherShare
if publisherShare.Decimal.LessThan(decimal.NewFromInt(0)) || publisherShare.Decimal.GreaterThan(decimal.NewFromInt(100)) {
// DO SOMETHING
return
}
我正在使用类型为 decimal.NullDecimal
(来自 github.com/shopspring/decimal
)的变量,并希望转换其整数值以便能够将其与整数进行比较。我应该如何转换它的十进制值?
这是我的代码:
import (
// other packages
"github.com/shopspring/decimal"
)
type BooksStores struct {
PublisherShare decimal.NullDecimal `json:"publisher_share"`
}
var booksStores BooksStores
err := c.ShouldBindBodyWith(&booksStores, binding.JSON)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"error": err.Error(),
})
return
}
publisherShare := booksStores.PublisherShare
// Here I want to compare the `publisherShare` value with 0 and 100
if publisherShare < 0 || publisherShare > 100 {
// DO SOMETHING
}
这是我尝试过的方法,但 none 没有成功:
import "https://github.com/spf13/cast"
cast.ToInte(publisherShare)
cast.ToInte(&publisherShare)
cast.ToInt(publisherShare.Decimal)
cast.ToInt(publisherShare.Decimal)
// but it is casted to zero, I have to use `decimal.NullDecimal` type
// due to the database considerations (later I have to update something)
我认为我应该使用 github.com/shopspring/decimal
包中的 Cmp
方法来比较 publisherShare
值与数字。
由于并非所有任意精度的小数都可以表示为 Go 整数,我想说你应该使用 decimal.NewFromInt(...)
将你的 int 转换为小数,然后使用 decimal.GreaterThan 之类的东西进行比较:
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
// Let's say dec is the decimal we want to compare
dec := decimal.NewFromInt(42)
// We compare it to 100
h := decimal.NewFromInt(100)
if dec.GreaterThan(h) {
fmt.Println("> 100")
} else {
fmt.Println("<= 100")
}
}
请注意,decimal.NullDecimal
仅包含一个 Decimal
和一个有效性标志,因此一旦您知道它是有效的,您只需从中获取 Decimal
字段,然后如上所示进行比较。
好的,现在问题解决了,我应该用var booksStores *BooksStores
而不是var booksStores BooksStores
。
type BooksStores struct {
PublisherShare decimal.NullDecimal `json:"publisher_share"`
}
var booksStores *BooksStores // Here is the place I had to change
err := c.ShouldBindBodyWith(&booksStores, binding.JSON)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"error": err.Error(),
})
return
}
publisherShare := booksStores.PublisherShare
if publisherShare.Decimal.LessThan(decimal.NewFromInt(0)) || publisherShare.Decimal.GreaterThan(decimal.NewFromInt(100)) {
// DO SOMETHING
return
}