当前位置:网站首页>golang json unsupported value: NaN 处理
golang json unsupported value: NaN 处理
2022-06-26 08:16:00 【用户昵称不能为空】
原因
golang 计算 math.Pow 导致数值变成了NaN
Python
使用Python计算没有这个问题
>>> sR = 100
>>> sV = -18.29
>>> pow(pow(1+sV/2,1/100),sR)
(-8.145000000000028+3.1086244689504383e-15j)
但使用GoLang 计算出来的结果是NaN
解决
var result float64
if fmt.Sprintf("%v",result) == "NaN" {
result = 0
}
或者
if math.IsNaN(result) {
result = 0
}
封装函数
func GetString(f float64, percent bool) (s string) {
if math.IsInf(f, 0) {
return "Inf"
}
if math.IsNaN(f) {
return "NaN"
}
suffix := ""
if percent {
f *= 100
suffix += "%"
}
s = fmt.Sprintf("%d%s", int(math.Round(f)), suffix)
return
}
边栏推荐
- GHUnit: Unit Testing Objective-C for the iPhone
- Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning
- [postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc
- "System error 5 occurred when win10 started mysql. Access denied"
- How to Use Instruments in Xcode
- Interview ES6
- Necessary protection ring for weak current detection
- Orderly arrangement
- MySQL practice: 1 Common database commands
- Mapping '/var/mobile/Library/Caches/com.apple.keyboards/images/tmp.gcyBAl37' failed: 'Invalid argume
猜你喜欢

Test method - decision table learning
![[postgraduate entrance examination] group planning: interrupted](/img/ec/1f3dc0ac22e3a80d721303864d2337.jpg)
[postgraduate entrance examination] group planning: interrupted

Rewrite string() method in go language

加密的JS代码,变量名能破解还原吗?

Wechat applet beginner level chapter

Chapter 5 (array)

Read excel table and render with FileReader object

buuresevewp
GHUnit: Unit Testing Objective-C for the iPhone

Multi interface switching in one UI of QT
随机推荐
Analysis of internal circuit of operational amplifier
buuresevewp
[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc
Listview control
Interview ES6
Flume learning notes
The difference between push-pull circuit drive and totem pole drive
Oracle database self study notes
Handwritten instanceof underlying principle
xxl-job配置告警邮件通知
MySQL practice: 2 Table definition and SQL classification
Necessary protection ring for weak current detection
Uni app installation and project directory (hbuilder configuration)
Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning
Interview JS and browser
1002: easy to remember phone number
Uni app is similar to Taobao in selecting multiple specifications of commodities (inventory judgment)
QT之一个UI里边多界面切换
Monitor iPad Keyboard Display and hide events
Hand drawn style chart library chart Implementation principle of xkcd