当前位置:网站首页>Golang 小数操作之判断几位小数点与四舍五入
Golang 小数操作之判断几位小数点与四舍五入
2022-07-31 16:34:00 【m0_67401228】
Golang 小数操作之判断几位小数点与四舍五入
Golang轻松学习
文章目录
一、判断几位小数点
1.判断几位小数点
加载 cast 包
go get github.com/spf13/cast
代码如下(示例):
package main
import (
"fmt"
"github.com/spf13/cast"
"math"
"regexp"
"strconv"
)
func main() {
num1 := 5.335
// 判断几位小数
decimal, _ := CheckDecimal(cast.ToString(num1), "2")
fmt.Println(decimal)
}
// CheckDecimal 判断小数点后几位小数点
// str 所要判断的 小数字符串
// n 所要判断的 位数
func CheckDecimal(str string, n string) (bool, error) {
// 通过 regexp 的 正则比较
// ([1-9]d*) 1-9 的任意位数
// (0) 或者第一位是 0
// d{0,`+n+`} 0-n 位数
return regexp.MatchString(`^(([1-9]d*)|(0))(.d{0,`+n+`})?$`, str)
}
二、四舍五入
1.fmt.Sprintf
测试:
5.334
5.335
5.336
代码如下(示例):
package main
import (
"fmt"
"github.com/spf13/cast"
"math"
"strconv"
)
func main() {
num1 := 5.334
fmt.Println(Float64Rand(num1,2))
}
//四舍五入
func Float64Rand(v float64, dig int) float64 {
cDig := strconv.Itoa(dig)
val := fmt.Sprintf("%0."+cDig+"f", v)
return cast.ToFloat64(val)
}
结果
从结果可以知道,官方包 fmt的四舍五入功能 不能达到我们的完美预期,四舍可以完成,但五入失败,只有当第三位成为六时才可生效
2.math.Round(result*pow)
测试:
5.334
5.335
5.336
代码如下(示例):
package main
import (
"fmt"
"github.com/spf13/cast"
"math"
"strconv"
)
func main() {
num1 := 5.334
fmt.Println("num1:",num1)
fmt.Println("res1:",FormatFloat(num1,2))
num2 := 5.335
fmt.Println("num2:",num2)
fmt.Println("res2:",FormatFloat(num2,2))
num3 := 5.336
fmt.Println("num3:",num3)
fmt.Println("res3:",FormatFloat(num3,2))
}
func FormatFloat(f float64, dig int) float64 {
result := cast.ToFloat64(strconv.FormatFloat(f, 'f', dig+1, 64))
pow := math.Pow(10, float64(dig))
return math.Round(result*pow) / pow
}
结果
从结果可以知道,math.Round(result*pow) 可以达到我们的完美预期,四舍五入可以完成。
总结
当官方包不足以满足我们的需要时,我们需要去寻找一个可以满足需要的方法来实现想要的功能,也将会在之后努力通过官方包实现四舍五入,让自己拓展知识面。
希望这个博客能对你有所益处。我是轻王,我为自己代言。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
- Qt实战案例(54)——利用QPixmap设计图片透明度
- tensorflow2.0 cnn(layerwise)
- 第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
- 并发性,时间和相对性
- LevelSequence源码分析
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- 动态规划之线性dp(上)
- adb shell 报错error: device unauthorized
- [pytorch] 1.7 pytorch and numpy, tensor and array conversion
猜你喜欢
二分查找的细节坑
GP 6总体架构学习笔记
6-22 Vulnerability exploit - postgresql database password cracking
[TypeScript] In-depth study of TypeScript type operations
Dialogue with Zhuang Biaowei: The first lesson of open source
C程序是如何跑起来的01 —— 普通可执行文件的构成
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
第二届中国PWA开发者日
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
adb shell 报错error: device unauthorized
随机推荐
How C programs run 01 - the composition of ordinary executable files
Anaconda如何顺利安装CV2
苹果官网样式调整 结账时产品图片“巨大化”
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
软件实现AT命令操作过程
仿生毛毛虫机器人源码
多数据中心操作和检测并发写入
基于C语言的编译器设计与实现
动态规划之线性dp(下)
form 表单提交后,使页面不跳转[通俗易懂]
研发过程中的文档管理与工具
6-22 Vulnerability exploit - postgresql database password cracking
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
【luogu P8326】Fliper(图论)(构造)(欧拉回路)
Design and Implementation of Compiler Based on C Language
arm按键控制led灯闪烁(嵌入式按键实验报告)
Summary of the implementation method of string inversion "recommended collection"
单细胞测序流程(单细胞rna测序)
adb shell 报错error: device unauthorized
入职一个月反思