Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将'decimal.NullDecimal'类型强制转换为int_Go_Casting - Fatal编程技术网

Go 将'decimal.NullDecimal'类型强制转换为int

Go 将'decimal.NullDecimal'类型强制转换为int,go,casting,Go,Casting,我正在使用一个类型为decimal.NullDecimal(来自github.com/shopspring/decimal)的变量,并希望强制转换其整数值,以便能够将其与整数进行比较。我应该如何转换其十进制值? 以下是我的代码: 导入( //其他包 “github.com/shopspring/decimal” ) 类型bookstores结构{ publisher共享decimal.NullDecimal`json:“publisher\u共享”` } var书店书店 错误:=c.Should

我正在使用一个类型为
decimal.NullDecimal
(来自
github.com/shopspring/decimal
)的变量,并希望强制转换其整数值,以便能够将其与整数进行比较。我应该如何转换其十进制值?
以下是我的代码:

导入(
//其他包
“github.com/shopspring/decimal”
)
类型bookstores结构{
publisher共享decimal.NullDecimal`json:“publisher\u共享”`
}
var书店书店
错误:=c.ShouldBindBodyWith(&bookstores,binding.JSON)
如果出错!=零{
c、 JSON(http.StatusUnprocessableEntity,gin.H{
“error”:err.error(),
})
返回
}
publisherShare:=bookstores.publisherShare
//在这里,我想将'publisherShare'值与0和100进行比较
如果publisherShare<0 | | publisherShare>100{
//做点什么
}
以下是我尝试过的,但没有一个没有成功:


进口”https://github.com/spf13/cast"
演员名单(出版商共享)
cast.ToInte(&publisherShare)
cast.ToInt(publisherShare.Decimal)
cast.ToInt(publisherShare.Decimal)
//但是它被强制转换为零,我必须使用'decimal.NullDecimal'类型
//由于数据库方面的考虑(稍后我必须更新一些内容)

我想我应该使用
github.com/shopspring/decimal
包上的
Cmp
方法来比较
publisherShare
值与数字。

由于并非所有任意精度的小数都可以表示为Go整数,我认为您可能应该使用
decimal.NewFromInt(…)将int转换为十进制
,然后使用以下方法进行比较:

主程序包
进口(
“fmt”
“github.com/shopspring/decimal”
)
func main(){
//假设dec是我们要比较的十进制数
dec:=十进制。NewFromInt(42)
//我们把它比作100
h:=十进制。NewFromInt(100)
如果12月大于(h){
格式打印项次(“>100”)
}否则{

fmt.Println(“好的,现在问题解决了,我应该使用
var bookstores*bookstores
而不是
var bookstores bookstores

类型bookstores结构{
publisher共享decimal.NullDecimal`json:“publisher\u共享”`
}
var bookstores*bookstores//这是我必须改变的地方
错误:=c.ShouldBindBodyWith(&bookstores,binding.JSON)
如果错误!=零{
c、 JSON(http.StatusUnprocessableEntity,gin.H{
“error”:err.error(),
})
返回
}
publisherShare:=bookstores.publisherShare
如果publisherShare.Decimal.LessThan(Decimal.NewFromInt(0))| | publisherShare.Decimal.greatethan(Decimal.NewFromInt(100)){
//做点什么
返回
}

关于术语的说明:Go不进行类型转换(尽管您使用的软件包名称错误)。您所追求的是类型转换。您跳过了主要问题,在
NewFromInt
方法中使用了整数(42)。问题是我无法获得
publisherShare
decimal.NewFromInt()的小数部分
需要一个int作为参数。当我使用
dec:=decimal.NewFromInt(publisherShare.decimal)时
由于使用十进制类型而不是int,我收到一个错误。@MajidAlaeinia:您在
PublisherShare
中已经有一个十进制。在您的情况下
dec:=PublisherShare.decimal
,否?问题现在解决了。我必须使用
var bookstores*bookstores
而不是
var bookstores bookstores
来获取实际的l十进制值,而不是其零值。
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")
    }
}