Commit 94765a4c by 杨树贤

还是精度问题

parent 5844fffa
Showing with 38 additions and 30 deletions
...@@ -57,14 +57,16 @@ func Sha1(query string, pri_key string) string { ...@@ -57,14 +57,16 @@ func Sha1(query string, pri_key string) string {
return query return query
} }
/** /*
@author wangsong *
获取请求过来的参数,并转为字符串,作用:为了有时候需要记录请求参数,压入日志
注意: @author wangsong
1.如果请求的是json,接收是需要读取body,而gin大部分函数只能读取一次body,读第二次会读不到 获取请求过来的参数,并转为字符串,作用:为了有时候需要记录请求参数,压入日志
而此函数需要频繁调用,所以需要在调用此函数之前,使用过ShouldBindBodyWith 方法,不然会读取不到。 注意:
具体需了解 ShouldBindBodyWith 源码 和 多次使用shouldbindjsond 的问题 1.如果请求的是json,接收是需要读取body,而gin大部分函数只能读取一次body,读第二次会读不到
2.表单提交不需要读body,则正常使用 而此函数需要频繁调用,所以需要在调用此函数之前,使用过ShouldBindBodyWith 方法,不然会读取不到。
具体需了解 ShouldBindBodyWith 源码 和 多次使用shouldbindjsond 的问题
2.表单提交不需要读body,则正常使用
*/ */
func GetRequestParam(ctx *gin.Context) string { func GetRequestParam(ctx *gin.Context) string {
ct := ctx.Request.Header.Get("Content-Type") ct := ctx.Request.Header.Get("Content-Type")
...@@ -141,7 +143,8 @@ func CopyStruct(struct1 interface{}, struct2 interface{}, Status ...int) { ...@@ -141,7 +143,8 @@ func CopyStruct(struct1 interface{}, struct2 interface{}, Status ...int) {
} }
} }
/** /*
*
@author wangsong @author wangsong
合并map,都有值以map1为主,字段类型以map1为主 合并map,都有值以map1为主,字段类型以map1为主
*/ */
...@@ -227,7 +230,7 @@ func Md5(str string) string { ...@@ -227,7 +230,7 @@ func Md5(str string) string {
return hex.EncodeToString(hash.Sum(nil)) return hex.EncodeToString(hash.Sum(nil))
} }
//非精确匹配,字符串截取向下80%,5个字符以下不截,5-10个截一个,10-20个向下取80%,20个以上向下取70% // 非精确匹配,字符串截取向下80%,5个字符以下不截,5-10个截一个,10-20个向下取80%,20个以上向下取70%
func SubKeyWordStr(str string) string { func SubKeyWordStr(str string) string {
strLen := len(str) strLen := len(str)
strLenFloat := float64(strLen) strLenFloat := float64(strLen)
...@@ -248,7 +251,7 @@ func SubKeyWordStr(str string) string { ...@@ -248,7 +251,7 @@ func SubKeyWordStr(str string) string {
return str return str
} }
//转成字符串的方法 // 转成字符串的方法
func ToString(a interface{}) string { func ToString(a interface{}) string {
if v, p := a.(int); p { if v, p := a.(int); p {
return strconv.Itoa(v) return strconv.Itoa(v)
...@@ -271,14 +274,14 @@ func ToString(a interface{}) string { ...@@ -271,14 +274,14 @@ func ToString(a interface{}) string {
return "change to String error" return "change to String error"
} }
//替换字符串,不区分大小写 // 替换字符串,不区分大小写
func CaseInsensitiveReplace(subject string, search string, replace string) string { func CaseInsensitiveReplace(subject string, search string, replace string) string {
searchRegex := regexp.MustCompile("(?i)" + search) searchRegex := regexp.MustCompile("(?i)" + search)
return searchRegex.ReplaceAllString(subject, replace) return searchRegex.ReplaceAllString(subject, replace)
} }
/* /*
md5字符串 md5字符串
*/ */
func GetKey(s string) string { func GetKey(s string) string {
return php2go.Md5(strings.ToLower(s)) return php2go.Md5(strings.ToLower(s))
...@@ -349,7 +352,7 @@ func StrRandom(lenNum int) string { ...@@ -349,7 +352,7 @@ func StrRandom(lenNum int) string {
return result return result
} }
//生成范围区间内的随机数 // 生成范围区间内的随机数
func Rand(min, max int) int { func Rand(min, max int) int {
n, _ := crand.Int(crand.Reader, big.NewInt(int64(max+1))) n, _ := crand.Int(crand.Reader, big.NewInt(int64(max+1)))
return int(n.Int64()) + min return int(n.Int64()) + min
...@@ -363,7 +366,7 @@ func CopyMapString(distmap map[string]string) map[string]string { ...@@ -363,7 +366,7 @@ func CopyMapString(distmap map[string]string) map[string]string {
return tmpmap return tmpmap
} }
//反爬虫加密验证 // 反爬虫加密验证
func CheckSignApi(ctx *gin.Context) (resNo int) { func CheckSignApi(ctx *gin.Context) (resNo int) {
params := make(map[string]string) params := make(map[string]string)
if ctx == nil { if ctx == nil {
...@@ -442,7 +445,7 @@ func CheckSignApi(ctx *gin.Context) (resNo int) { ...@@ -442,7 +445,7 @@ func CheckSignApi(ctx *gin.Context) (resNo int) {
return 3 return 3
} }
//内部免验证通过 // 内部免验证通过
func auth(ctx *gin.Context) bool { func auth(ctx *gin.Context) bool {
if ctx == nil { if ctx == nil {
return false return false
...@@ -498,7 +501,7 @@ func isSlice(arg interface{}) (val reflect.Value, ok bool) { ...@@ -498,7 +501,7 @@ func isSlice(arg interface{}) (val reflect.Value, ok bool) {
return return
} }
//向左补充字符串 // 向左补充字符串
func StrPadLeft(input string, padLength int, padString string) string { func StrPadLeft(input string, padLength int, padString string) string {
output := padString output := padString
...@@ -513,7 +516,7 @@ func StrPadLeft(input string, padLength int, padString string) string { ...@@ -513,7 +516,7 @@ func StrPadLeft(input string, padLength int, padString string) string {
return output[:padLength-len(input)] + input return output[:padLength-len(input)] + input
} }
//移除字符串切片中某個元素 // 移除字符串切片中某個元素
func RemoveSliceString(s []string, i string) []string { func RemoveSliceString(s []string, i string) []string {
b := make([]string, 0) b := make([]string, 0)
for _, v := range s { for _, v := range s {
...@@ -525,7 +528,7 @@ func RemoveSliceString(s []string, i string) []string { ...@@ -525,7 +528,7 @@ func RemoveSliceString(s []string, i string) []string {
return b return b
} }
//map排序 // map排序
func MapSort(mapList map[int]int) []int { func MapSort(mapList map[int]int) []int {
var ( var (
keys []int keys []int
...@@ -544,7 +547,7 @@ func MapSort(mapList map[int]int) []int { ...@@ -544,7 +547,7 @@ func MapSort(mapList map[int]int) []int {
return newList return newList
} }
////////////类型转换///////////////////// // //////////类型转换/////////////////////
func MyInt(str string) int { func MyInt(str string) int {
res, _ := strconv.ParseInt(str, 10, 64) res, _ := strconv.ParseInt(str, 10, 64)
return int(res) return int(res)
...@@ -580,7 +583,8 @@ func MyIntToStr(i int) string { ...@@ -580,7 +583,8 @@ func MyIntToStr(i int) string {
} }
/* /*
四舍五入取多少位 四舍五入取多少位
@param float64 x 目标分析字符串 @param float64 x 目标分析字符串
@param int wei 取小数多少位 @param int wei 取小数多少位
*/ */
...@@ -589,12 +593,16 @@ func MyRound(x float64, wei int) float64 { ...@@ -589,12 +593,16 @@ func MyRound(x float64, wei int) float64 {
return math.Floor(x + 0.5) return math.Floor(x + 0.5)
} }
weis := map[int]float64{ weis := map[int]float64{
1: 10, 1: 10,
2: 100, 2: 100,
3: 1000, 3: 1000,
4: 10000, 4: 10000,
5: 100000, 5: 100000,
6: 1000000, 6: 1000000,
7: 10000000,
8: 100000000,
9: 1000000000,
10: 1000000000,
} }
weishu := weis[wei] weishu := weis[wei]
a := math.Floor(x*weishu+0.5) / weishu a := math.Floor(x*weishu+0.5) / weishu
...@@ -603,7 +611,7 @@ func MyRound(x float64, wei int) float64 { ...@@ -603,7 +611,7 @@ func MyRound(x float64, wei int) float64 {
////////////类型转换///////////////////// ////////////类型转换/////////////////////
//浮点数乘法 // 浮点数乘法
func MulFloat(first float64, args ...float64) float64 { func MulFloat(first float64, args ...float64) float64 {
var result float64 var result float64
result = first result = first
...@@ -613,7 +621,7 @@ func MulFloat(first float64, args ...float64) float64 { ...@@ -613,7 +621,7 @@ func MulFloat(first float64, args ...float64) float64 {
return result return result
} }
//浮点除法 // 浮点除法
func DivFloat(first float64, second float64) float64 { func DivFloat(first float64, second float64) float64 {
return ((first * 1000000000) / (second * 1000000000)) return ((first * 1000000000) / (second * 1000000000))
} }
...@@ -661,7 +661,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku, priceUs f ...@@ -661,7 +661,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku, priceUs f
//fmt.Println(rmbRatio, usRatio) //fmt.Println(rmbRatio, usRatio)
//人民币汇率转美金汇率 //人民币汇率转美金汇率
usRatio = c.MyRound(c.DivFloat(rmbRatio, usRatio), 6) usRatio = c.MyRound(c.DivFloat(rmbRatio, usRatio), 10)
priceUs = c.MyRound(c.MulFloat(priceUs, usRatio), 4) priceUs = c.MyRound(c.MulFloat(priceUs, usRatio), 4)
} }
return priceUs return priceUs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment