common.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. package utils
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. cryRand "crypto/rand"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "github.com/mozillazg/go-pinyin"
  13. "github.com/shopspring/decimal"
  14. "image"
  15. "image/png"
  16. "io"
  17. "io/ioutil"
  18. "math"
  19. "math/big"
  20. "math/rand"
  21. "net"
  22. "net/http"
  23. "net/http/cookiejar"
  24. "os"
  25. "os/exec"
  26. "path"
  27. "regexp"
  28. "strconv"
  29. "strings"
  30. "time"
  31. "unicode"
  32. )
  33. // 随机数种子
  34. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  35. func GetRandString(size int) string {
  36. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
  37. randomSb := ""
  38. digitSize := len(allLetterDigit)
  39. for i := 0; i < size; i++ {
  40. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  41. }
  42. return randomSb
  43. }
  44. func GetRandStringNoSpecialChar(size int) string {
  45. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  46. randomSb := ""
  47. digitSize := len(allLetterDigit)
  48. for i := 0; i < size; i++ {
  49. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  50. }
  51. return randomSb
  52. }
  53. func StringsToJSON(str string) string {
  54. rs := []rune(str)
  55. jsons := ""
  56. for _, r := range rs {
  57. rint := int(r)
  58. if rint < 128 {
  59. jsons += string(r)
  60. } else {
  61. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  62. }
  63. }
  64. return jsons
  65. }
  66. // 序列化
  67. func ToString(v interface{}) string {
  68. data, _ := json.Marshal(v)
  69. return string(data)
  70. }
  71. // md5加密
  72. func MD5(data string) string {
  73. m := md5.Sum([]byte(data))
  74. return hex.EncodeToString(m[:])
  75. }
  76. // 获取数字随机字符
  77. func GetRandDigit(n int) string {
  78. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  79. }
  80. // 获取随机数
  81. func GetRandNumber(n int) int {
  82. return rnd.Intn(n)
  83. }
  84. func GetRandInt(min, max int) int {
  85. if min >= max || min == 0 || max == 0 {
  86. return max
  87. }
  88. return rand.Intn(max-min) + min
  89. }
  90. func GetToday(format string) string {
  91. today := time.Now().Format(format)
  92. return today
  93. }
  94. // 获取今天剩余秒数
  95. func GetTodayLastSecond() time.Duration {
  96. today := GetToday(FormatDate) + " 23:59:59"
  97. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  98. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  99. }
  100. // 处理出生日期函数
  101. func GetBrithDate(idcard string) string {
  102. l := len(idcard)
  103. var s string
  104. if l == 15 {
  105. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  106. return s
  107. }
  108. if l == 18 {
  109. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  110. return s
  111. }
  112. return GetToday(FormatDate)
  113. }
  114. // 处理性别
  115. func WhichSexByIdcard(idcard string) string {
  116. var sexs = [2]string{"女", "男"}
  117. length := len(idcard)
  118. if length == 18 {
  119. sex, _ := strconv.Atoi(string(idcard[16]))
  120. return sexs[sex%2]
  121. } else if length == 15 {
  122. sex, _ := strconv.Atoi(string(idcard[14]))
  123. return sexs[sex%2]
  124. }
  125. return "男"
  126. }
  127. // 截取小数点后几位
  128. func SubFloatToString(f float64, m int) string {
  129. n := strconv.FormatFloat(f, 'f', -1, 64)
  130. if n == "" {
  131. return ""
  132. }
  133. if m >= len(n) {
  134. return n
  135. }
  136. newn := strings.Split(n, ".")
  137. if m == 0 {
  138. return newn[0]
  139. }
  140. if len(newn) < 2 || m >= len(newn[1]) {
  141. return n
  142. }
  143. return newn[0] + "." + newn[1][:m]
  144. }
  145. // 截取小数点后几位
  146. func SubFloatToFloat(f float64, m int) float64 {
  147. newn := SubFloatToString(f, m)
  148. newf, _ := strconv.ParseFloat(newn, 64)
  149. return newf
  150. }
  151. // 截取小数点后几位
  152. func SubFloatToFloatStr(f float64, m int) string {
  153. newn := SubFloatToString(f, m)
  154. return newn
  155. }
  156. // 获取相差时间-年
  157. func GetYearDiffer(start_time, end_time string) int {
  158. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  159. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  160. age := t2.Year() - t1.Year()
  161. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  162. age--
  163. }
  164. return age
  165. }
  166. // 获取相差时间-秒
  167. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  168. diff := end_time.Unix() - start_time.Unix()
  169. return diff
  170. }
  171. func FixFloat(f float64, m int) float64 {
  172. newn := SubFloatToString(f+0.00000001, m)
  173. newf, _ := strconv.ParseFloat(newn, 64)
  174. return newf
  175. }
  176. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  177. func StrListToString(strList []string) (str string) {
  178. if len(strList) > 0 {
  179. for k, v := range strList {
  180. if k == 0 {
  181. str = v
  182. } else {
  183. str = str + "," + v
  184. }
  185. }
  186. return
  187. }
  188. return ""
  189. }
  190. // Token
  191. func GetToken() string {
  192. randStr := GetRandString(64)
  193. token := MD5(randStr + Md5Key)
  194. tokenLen := 64 - len(token)
  195. return strings.ToUpper(token + GetRandString(tokenLen))
  196. }
  197. // 数据没有记录
  198. func ErrNoRow() string {
  199. return "<QuerySeter> no row found"
  200. }
  201. // 判断文件是否存在
  202. func FileIsExist(filePath string) bool {
  203. _, err := os.Stat(filePath)
  204. return err == nil || os.IsExist(err)
  205. }
  206. // 获取图片扩展名
  207. func GetImgExt(file string) (ext string, err error) {
  208. var headerByte []byte
  209. headerByte = make([]byte, 8)
  210. fd, err := os.Open(file)
  211. if err != nil {
  212. return "", err
  213. }
  214. defer fd.Close()
  215. _, err = fd.Read(headerByte)
  216. if err != nil {
  217. return "", err
  218. }
  219. xStr := fmt.Sprintf("%x", headerByte)
  220. switch {
  221. case xStr == "89504e470d0a1a0a":
  222. ext = ".png"
  223. case xStr == "0000010001002020":
  224. ext = ".ico"
  225. case xStr == "0000020001002020":
  226. ext = ".cur"
  227. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  228. ext = ".gif"
  229. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  230. ext = ".tga"
  231. case xStr[:8] == "464f524d":
  232. ext = ".iff"
  233. case xStr[:8] == "52494646":
  234. ext = ".ani"
  235. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  236. ext = ".tiff"
  237. case xStr[:4] == "424d":
  238. ext = ".bmp"
  239. case xStr[:4] == "ffd8":
  240. ext = ".jpg"
  241. case xStr[:2] == "0a":
  242. ext = ".pcx"
  243. default:
  244. ext = ""
  245. }
  246. return ext, nil
  247. }
  248. // 保存图片
  249. func SaveImage(path string, img image.Image) (err error) {
  250. //需要保持的文件
  251. imgfile, err := os.Create(path)
  252. defer imgfile.Close()
  253. // 以PNG格式保存文件
  254. err = png.Encode(imgfile, img)
  255. return err
  256. }
  257. // 下载图片
  258. func DownloadImage(imgUrl string) (filePath string, err error) {
  259. imgPath := "./static/imgs/"
  260. fileName := path.Base(imgUrl)
  261. res, err := http.Get(imgUrl)
  262. if err != nil {
  263. fmt.Println("A error occurred!")
  264. return
  265. }
  266. defer res.Body.Close()
  267. // 获得get请求响应的reader对象
  268. reader := bufio.NewReaderSize(res.Body, 32*1024)
  269. filePath = imgPath + fileName
  270. file, err := os.Create(filePath)
  271. if err != nil {
  272. return
  273. }
  274. // 获得文件的writer对象
  275. writer := bufio.NewWriter(file)
  276. written, _ := io.Copy(writer, reader)
  277. fmt.Printf("Total length: %d \n", written)
  278. return
  279. }
  280. // 保存base64数据为文件
  281. func SaveBase64ToFile(content, path string) error {
  282. data, err := base64.StdEncoding.DecodeString(content)
  283. if err != nil {
  284. return err
  285. }
  286. f, err := os.Create(path)
  287. defer f.Close()
  288. if err != nil {
  289. return err
  290. }
  291. f.Write(data)
  292. return nil
  293. }
  294. func SaveBase64ToFileBySeek(content, path string) (err error) {
  295. data, err := base64.StdEncoding.DecodeString(content)
  296. exist, err := PathExists(path)
  297. if err != nil {
  298. return
  299. }
  300. if !exist {
  301. f, err := os.Create(path)
  302. if err != nil {
  303. return err
  304. }
  305. n, _ := f.Seek(0, 2)
  306. // 从末尾的偏移量开始写入内容
  307. _, err = f.WriteAt([]byte(data), n)
  308. defer f.Close()
  309. } else {
  310. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  311. if err != nil {
  312. return err
  313. }
  314. n, _ := f.Seek(0, 2)
  315. // 从末尾的偏移量开始写入内容
  316. _, err = f.WriteAt([]byte(data), n)
  317. defer f.Close()
  318. }
  319. return nil
  320. }
  321. func PathExists(path string) (bool, error) {
  322. _, err := os.Stat(path)
  323. if err == nil {
  324. return true, nil
  325. }
  326. if os.IsNotExist(err) {
  327. return false, nil
  328. }
  329. return false, err
  330. }
  331. func StartIndex(page, pagesize int) int {
  332. if page > 1 {
  333. return (page - 1) * pagesize
  334. }
  335. return 0
  336. }
  337. func PageCount(count, pagesize int) int {
  338. if count%pagesize > 0 {
  339. return count/pagesize + 1
  340. } else {
  341. return count / pagesize
  342. }
  343. }
  344. func TrimHtml(src string) string {
  345. //将HTML标签全转换成小写
  346. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  347. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  348. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  349. src = re.ReplaceAllString(src, "[图片]")
  350. re, _ = regexp.Compile("class[\\S\\s]+?>")
  351. src = re.ReplaceAllString(src, "")
  352. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  353. src = re.ReplaceAllString(src, "")
  354. return strings.TrimSpace(src)
  355. }
  356. //1556164246 -> 2019-04-25 03:50:46 +0000
  357. //timestamp
  358. func TimeToTimestamp() {
  359. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  360. }
  361. func ToUnicode(text string) string {
  362. textQuoted := strconv.QuoteToASCII(text)
  363. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  364. return textUnquoted
  365. }
  366. func VersionToInt(version string) int {
  367. version = strings.Replace(version, ".", "", -1)
  368. n, _ := strconv.Atoi(version)
  369. return n
  370. }
  371. func IsCheckInList(list []int, s int) bool {
  372. for _, v := range list {
  373. if v == s {
  374. return true
  375. }
  376. }
  377. return false
  378. }
  379. func round(num float64) int {
  380. return int(num + math.Copysign(0.5, num))
  381. }
  382. func toFixed(num float64, precision int) float64 {
  383. output := math.Pow(10, float64(precision))
  384. return float64(round(num*output)) / output
  385. }
  386. // GetWilsonScore returns Wilson Score
  387. func GetWilsonScore(p, n float64) float64 {
  388. if p == 0 && n == 0 {
  389. return 0
  390. }
  391. return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
  392. }
  393. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  394. func ChangeWordsToNum(str string) (numStr string) {
  395. words := ([]rune)(str)
  396. num := 0
  397. n := 0
  398. for i := 0; i < len(words); i++ {
  399. word := string(words[i : i+1])
  400. switch word {
  401. case "万":
  402. if n == 0 {
  403. n = 1
  404. }
  405. n = n * 10000
  406. num = num*10000 + n
  407. n = 0
  408. case "千":
  409. if n == 0 {
  410. n = 1
  411. }
  412. n = n * 1000
  413. num += n
  414. n = 0
  415. case "百":
  416. if n == 0 {
  417. n = 1
  418. }
  419. n = n * 100
  420. num += n
  421. n = 0
  422. case "十":
  423. if n == 0 {
  424. n = 1
  425. }
  426. n = n * 10
  427. num += n
  428. n = 0
  429. case "一":
  430. n += 1
  431. case "二":
  432. n += 2
  433. case "三":
  434. n += 3
  435. case "四":
  436. n += 4
  437. case "五":
  438. n += 5
  439. case "六":
  440. n += 6
  441. case "七":
  442. n += 7
  443. case "八":
  444. n += 8
  445. case "九":
  446. n += 9
  447. case "零":
  448. default:
  449. if n > 0 {
  450. num += n
  451. n = 0
  452. }
  453. if num == 0 {
  454. numStr += word
  455. } else {
  456. numStr += strconv.Itoa(num) + word
  457. num = 0
  458. }
  459. }
  460. }
  461. if n > 0 {
  462. num += n
  463. n = 0
  464. }
  465. if num != 0 {
  466. numStr += strconv.Itoa(num)
  467. }
  468. return
  469. }
  470. func Sha1(data string) string {
  471. sha1 := sha1.New()
  472. sha1.Write([]byte(data))
  473. return hex.EncodeToString(sha1.Sum([]byte("")))
  474. }
  475. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  476. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  477. out, err := exec.Command("bash", "-c", cmd).Output()
  478. if err != nil {
  479. return
  480. }
  481. outTimes := string(out)
  482. fmt.Println("outTimes:", outTimes)
  483. if outTimes != "" {
  484. timeArr := strings.Split(outTimes, ":")
  485. h := timeArr[0]
  486. m := timeArr[1]
  487. s := timeArr[2]
  488. hInt, err := strconv.Atoi(h)
  489. if err != nil {
  490. return playSeconds, err
  491. }
  492. mInt, err := strconv.Atoi(m)
  493. if err != nil {
  494. return playSeconds, err
  495. }
  496. s = strings.Trim(s, " ")
  497. s = strings.Trim(s, "\n")
  498. sInt, err := strconv.ParseFloat(s, 64)
  499. if err != nil {
  500. return playSeconds, err
  501. }
  502. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  503. }
  504. return
  505. }
  506. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  507. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  508. tradeCode = strings.Trim(tradeCode, " ")
  509. tradeCodeInt, err := strconv.Atoi(tradeCode)
  510. if err != nil {
  511. return
  512. }
  513. tradeCodeInt = tradeCodeInt + 1
  514. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  515. return
  516. }
  517. // excel日期字段格式化 yyyy-mm-dd
  518. func ConvertToFormatDay(excelDaysString string) string {
  519. // 2006-01-02 距离 1900-01-01的天数
  520. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  521. curDiffDay := excelDaysString
  522. b, _ := strconv.Atoi(curDiffDay)
  523. // 获取excel的日期距离2006-01-02的天数
  524. realDiffDay := b - baseDiffDay
  525. //fmt.Println("realDiffDay:",realDiffDay)
  526. // 距离2006-01-02 秒数
  527. realDiffSecond := realDiffDay * 24 * 3600
  528. //fmt.Println("realDiffSecond:",realDiffSecond)
  529. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  530. baseOriginSecond := 1136185445
  531. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  532. return resultTime
  533. }
  534. func CheckPwd(pwd string) bool {
  535. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  536. reg := regexp.MustCompile(compile)
  537. flag := reg.MatchString(pwd)
  538. return flag
  539. }
  540. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  541. // 数字月份必须前置补零
  542. if len(myMonth) == 1 {
  543. myMonth = "0" + myMonth
  544. }
  545. yInt, _ := strconv.Atoi(myYear)
  546. timeLayout := "2006-01-02 15:04:05"
  547. loc, _ := time.LoadLocation("Local")
  548. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  549. newMonth := theTime.Month()
  550. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  551. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  552. return t1, t2
  553. }
  554. // 移除字符串中的空格
  555. func TrimStr(str string) (str2 string) {
  556. return strings.Replace(str, " ", "", -1)
  557. }
  558. // 字符串转换为time
  559. func StrTimeToTime(strTime string) time.Time {
  560. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  561. loc, _ := time.LoadLocation("Local") //重要:获取时区
  562. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  563. return resultTime
  564. }
  565. // 字符串类型时间转周几
  566. func StrDateTimeToWeek(strTime string) string {
  567. var WeekDayMap = map[string]string{
  568. "Monday": "周一",
  569. "Tuesday": "周二",
  570. "Wednesday": "周三",
  571. "Thursday": "周四",
  572. "Friday": "周五",
  573. "Saturday": "周六",
  574. "Sunday": "周日",
  575. }
  576. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  577. startday, _ := time.Parse("2006-01-02", ctime)
  578. staweek_int := startday.Weekday().String()
  579. return WeekDayMap[staweek_int]
  580. }
  581. // 时间格式转年月日字符串
  582. func TimeToStrYmd(time2 time.Time) string {
  583. var Ymd string
  584. year := time2.Year()
  585. month := time2.Format("1")
  586. day1 := time.Now().Day()
  587. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  588. return Ymd
  589. }
  590. // 时间格式去掉时分秒
  591. func TimeRemoveHms(strTime string) string {
  592. var Ymd string
  593. var resultTime = StrTimeToTime(strTime)
  594. year := resultTime.Year()
  595. month := resultTime.Format("01")
  596. day1 := resultTime.Day()
  597. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  598. return Ymd
  599. }
  600. // 文章上一次编辑时间
  601. func ArticleLastTime(strTime string) string {
  602. var newTime string
  603. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  604. diffTime := time.Now().Unix() - stamp.Unix()
  605. if diffTime <= 60 {
  606. newTime = "当前"
  607. } else if diffTime < 60*60 {
  608. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  609. } else if diffTime < 24*60*60 {
  610. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  611. } else if diffTime < 30*24*60*60 {
  612. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  613. } else if diffTime < 12*30*24*60*60 {
  614. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  615. } else {
  616. newTime = "1年前"
  617. }
  618. return newTime
  619. }
  620. // 人民币小写转大写
  621. func ConvertNumToCny(num float64) (str string, err error) {
  622. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  623. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  624. // log.Println(sliceUnit[:len(sliceUnit)-2])
  625. s := sliceUnit[len(sliceUnit)-len(strNum):]
  626. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  627. for k, v := range strNum[:] {
  628. str = str + upperDigitUnit[string(v)] + s[k]
  629. }
  630. reg, err := regexp.Compile(`零角零分$`)
  631. str = reg.ReplaceAllString(str, "整")
  632. reg, err = regexp.Compile(`零角`)
  633. str = reg.ReplaceAllString(str, "零")
  634. reg, err = regexp.Compile(`零分$`)
  635. str = reg.ReplaceAllString(str, "整")
  636. reg, err = regexp.Compile(`零[仟佰拾]`)
  637. str = reg.ReplaceAllString(str, "零")
  638. reg, err = regexp.Compile(`零{2,}`)
  639. str = reg.ReplaceAllString(str, "零")
  640. reg, err = regexp.Compile(`零亿`)
  641. str = reg.ReplaceAllString(str, "亿")
  642. reg, err = regexp.Compile(`零万`)
  643. str = reg.ReplaceAllString(str, "万")
  644. reg, err = regexp.Compile(`零*元`)
  645. str = reg.ReplaceAllString(str, "元")
  646. reg, err = regexp.Compile(`亿零{0, 3}万`)
  647. str = reg.ReplaceAllString(str, "^元")
  648. reg, err = regexp.Compile(`零元`)
  649. str = reg.ReplaceAllString(str, "零")
  650. return
  651. }
  652. // GetNowWeekMonday 获取本周周一的时间
  653. func GetNowWeekMonday() time.Time {
  654. offset := int(time.Monday - time.Now().Weekday())
  655. mondayTime := time.Now().AddDate(0, 0, offset)
  656. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  657. return mondayTime
  658. }
  659. // GetNowWeekLastDay 获取本周最后一天的时间
  660. func GetNowWeekLastDay() time.Time {
  661. offset := int(time.Monday - time.Now().Weekday())
  662. firstDayTime := time.Now().AddDate(0, 0, offset)
  663. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  664. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  665. return lastDayTime
  666. }
  667. // GetNowMonthFirstDay 获取本月第一天的时间
  668. func GetNowMonthFirstDay() time.Time {
  669. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  670. return nowMonthFirstDay
  671. }
  672. // GetNowMonthLastDay 获取本月最后一天的时间
  673. func GetNowMonthLastDay() time.Time {
  674. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  675. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  676. return nowMonthLastDay
  677. }
  678. // GetNowQuarterFirstDay 获取本季度第一天的时间
  679. func GetNowQuarterFirstDay() time.Time {
  680. month := int(time.Now().Month())
  681. var nowQuarterFirstDay time.Time
  682. if month >= 1 && month <= 3 {
  683. //1月1号
  684. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  685. } else if month >= 4 && month <= 6 {
  686. //4月1号
  687. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  688. } else if month >= 7 && month <= 9 {
  689. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  690. } else {
  691. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  692. }
  693. return nowQuarterFirstDay
  694. }
  695. // GetNowQuarterLastDay 获取本季度最后一天的时间
  696. func GetNowQuarterLastDay() time.Time {
  697. month := int(time.Now().Month())
  698. var nowQuarterLastDay time.Time
  699. if month >= 1 && month <= 3 {
  700. //03-31 23:59:59
  701. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  702. } else if month >= 4 && month <= 6 {
  703. //06-30 23:59:59
  704. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  705. } else if month >= 7 && month <= 9 {
  706. //09-30 23:59:59
  707. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  708. } else {
  709. //12-31 23:59:59
  710. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  711. }
  712. return nowQuarterLastDay
  713. }
  714. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  715. func GetNowHalfYearFirstDay() time.Time {
  716. month := int(time.Now().Month())
  717. var nowHalfYearLastDay time.Time
  718. if month >= 1 && month <= 6 {
  719. //03-31 23:59:59
  720. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  721. } else {
  722. //12-31 23:59:59
  723. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  724. }
  725. return nowHalfYearLastDay
  726. }
  727. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  728. func GetNowHalfYearLastDay() time.Time {
  729. month := int(time.Now().Month())
  730. var nowHalfYearLastDay time.Time
  731. if month >= 1 && month <= 6 {
  732. //03-31 23:59:59
  733. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  734. } else {
  735. //12-31 23:59:59
  736. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  737. }
  738. return nowHalfYearLastDay
  739. }
  740. // GetNowYearFirstDay 获取当前年的最后一天的时间
  741. func GetNowYearFirstDay() time.Time {
  742. //12-31 23:59:59
  743. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  744. return nowYearFirstDay
  745. }
  746. // GetNowYearLastDay 获取当前年的最后一天的时间
  747. func GetNowYearLastDay() time.Time {
  748. //12-31 23:59:59
  749. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  750. return nowYearLastDay
  751. }
  752. // CalculationDate 计算两个日期之间相差n年m月y天
  753. func CalculationDate(startDate, endDate time.Time) (beetweenDay string, err error) {
  754. //startDate := time.Date(2021, 3, 28, 0, 0, 0, 0, time.Now().Location())
  755. //endDate := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Now().Location())
  756. numYear := endDate.Year() - startDate.Year()
  757. numMonth := int(endDate.Month()) - int(startDate.Month())
  758. numDay := 0
  759. //获取截止月的总天数
  760. endDateDays := getMonthDay(endDate.Year(), int(endDate.Month()))
  761. //获取截止月的前一个月
  762. endDatePrevMonthDate := endDate.AddDate(0, -1, 0)
  763. //获取截止日期的上一个月的总天数
  764. endDatePrevMonthDays := getMonthDay(endDatePrevMonthDate.Year(), int(endDatePrevMonthDate.Month()))
  765. //获取开始日期的的月份总天数
  766. startDateMonthDays := getMonthDay(startDate.Year(), int(startDate.Month()))
  767. //判断,截止月是否完全被选中,如果相等,那么代表截止月份全部天数被选择
  768. if endDate.Day() == endDateDays {
  769. numDay = startDateMonthDays - startDate.Day() + 1
  770. //如果剩余天数正好与开始日期的天数是一致的,那么月份加1
  771. if numDay == startDateMonthDays {
  772. numMonth++
  773. numDay = 0
  774. //超过月份了,那么年份加1
  775. if numMonth == 12 {
  776. numYear++
  777. numMonth = 0
  778. }
  779. }
  780. } else {
  781. numDay = endDate.Day() - startDate.Day() + 1
  782. }
  783. //天数小于0,那么向月份借一位
  784. if numDay < 0 {
  785. //向上一个月借一个月的天数
  786. numDay += endDatePrevMonthDays
  787. //总月份减去一个月
  788. numMonth = numMonth - 1
  789. }
  790. //月份小于0,那么向年份借一位
  791. if numMonth < 0 {
  792. //向上一个年借12个月
  793. numMonth += 12
  794. //总年份减去一年
  795. numYear = numYear - 1
  796. }
  797. if numYear < 0 {
  798. err = errors.New("日期异常")
  799. return
  800. }
  801. if numYear > 0 {
  802. beetweenDay += fmt.Sprint(numYear, "年")
  803. }
  804. if numMonth > 0 {
  805. beetweenDay += fmt.Sprint(numMonth, "个月")
  806. }
  807. if numDay > 0 {
  808. beetweenDay += fmt.Sprint(numDay, "天")
  809. }
  810. return
  811. }
  812. // getMonthDay 获取某年某月有多少天
  813. func getMonthDay(year, month int) (days int) {
  814. if month != 2 {
  815. if month == 4 || month == 6 || month == 9 || month == 11 {
  816. days = 30
  817. } else {
  818. days = 31
  819. }
  820. } else {
  821. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  822. days = 29
  823. } else {
  824. days = 28
  825. }
  826. }
  827. return
  828. }
  829. // GetOrmInReplace 获取orm的in查询替换?的方法
  830. func GetOrmInReplace(num int) string {
  831. template := make([]string, num)
  832. for i := 0; i < num; i++ {
  833. template[i] = "?"
  834. }
  835. return strings.Join(template, ",")
  836. }
  837. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该int值)
  838. func InArrayByInt(idIntList []int, searchId int) (has bool) {
  839. for _, id := range idIntList {
  840. if id == searchId {
  841. has = true
  842. return
  843. }
  844. }
  845. return
  846. }
  847. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  848. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  849. for _, id := range idStrList {
  850. if id == searchId {
  851. has = true
  852. return
  853. }
  854. }
  855. return
  856. }
  857. // RangeRand 取区间随机数
  858. func RangeRand(min, max int64) int64 {
  859. if min > max {
  860. return max
  861. }
  862. if min < 0 {
  863. f64Min := math.Abs(float64(min))
  864. i64Min := int64(f64Min)
  865. result, _ := cryRand.Int(cryRand.Reader, big.NewInt(max+1+i64Min))
  866. return result.Int64() - i64Min
  867. } else {
  868. result, _ := cryRand.Int(cryRand.Reader, big.NewInt(max-min+1))
  869. return min + result.Int64()
  870. }
  871. }
  872. func GetLocalIP() (ip string, err error) {
  873. addrs, err := net.InterfaceAddrs()
  874. if err != nil {
  875. return
  876. }
  877. for _, addr := range addrs {
  878. ipAddr, ok := addr.(*net.IPNet)
  879. if !ok {
  880. continue
  881. }
  882. if ipAddr.IP.IsLoopback() {
  883. continue
  884. }
  885. if !ipAddr.IP.IsGlobalUnicast() {
  886. continue
  887. }
  888. return ipAddr.IP.String(), nil
  889. }
  890. return
  891. }
  892. // HTTP: post请求参数
  893. //
  894. // {
  895. // "A" : 10086,
  896. // "B" : "请求信息"
  897. // }
  898. type RequestParam struct {
  899. A int `json:"A"`
  900. B string `json:"B"`
  901. }
  902. // HTTP返回Body
  903. type HTTPRspBody struct {
  904. Result Results `json:"Result"`
  905. }
  906. type Results struct {
  907. RequestID string `json:"Result"`
  908. HasError bool `json:"HasError"`
  909. ResponseItems ErrorMsg `json:"ResponseItems"`
  910. }
  911. type ErrorMsg struct {
  912. ErrorMsg string `json:"ErrorMsg"`
  913. }
  914. // 修改供应商信息
  915. func HttpPost(url string, reqParam string, headersParams map[string]string) ([]byte, error) {
  916. var (
  917. err error
  918. )
  919. // 准备: HTTP请求
  920. reqBody := strings.NewReader(reqParam)
  921. httpReq, err := http.NewRequest("POST", url, reqBody)
  922. if err != nil {
  923. fmt.Printf("NewRequest fail, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  924. return nil, err
  925. }
  926. for k, v := range headersParams {
  927. httpReq.Header.Set(k, v)
  928. }
  929. //httpReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
  930. jar, err := cookiejar.New(nil)
  931. if err != nil {
  932. panic(err)
  933. }
  934. //httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
  935. client := &http.Client{Jar: jar}
  936. // DO: HTTP请求
  937. httpRsp, err := client.Do(httpReq)
  938. if err != nil {
  939. fmt.Printf("do http fail, url: %s, reqBody: %s, err:%v", url, reqBody, err)
  940. return nil, err
  941. }
  942. defer httpRsp.Body.Close()
  943. // Read: HTTP结果
  944. b, err := ioutil.ReadAll(httpRsp.Body)
  945. if err != nil {
  946. fmt.Printf("ReadAll failed, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  947. return nil, err
  948. }
  949. // unmarshal: 解析HTTP返回的结果
  950. // body: {"Result":{"RequestId":"12131","HasError":true,"ResponseItems":{"ErrorMsg":"错误信息"}}}
  951. return b, err
  952. }
  953. func HttpGet(reqUrl, payload string) (err error) {
  954. client := &http.Client{}
  955. req, err := http.NewRequest("GET", reqUrl, strings.NewReader(payload))
  956. if err != nil {
  957. return
  958. }
  959. res, err := client.Do(req)
  960. if err != nil {
  961. return
  962. }
  963. defer res.Body.Close()
  964. _, err = ioutil.ReadAll(res.Body)
  965. if err != nil {
  966. return
  967. }
  968. Cookie := res.Header.Get("Cookie")
  969. fmt.Println(Cookie)
  970. rcookie := req.Header.Get("Cookie")
  971. fmt.Println("rcookie")
  972. fmt.Println(rcookie)
  973. //fmt.Println("body:" + string(body))
  974. cookiesArr := res.Cookies()
  975. fmt.Println("cookiesArrLen:", len(cookiesArr))
  976. for k, v := range cookiesArr {
  977. fmt.Println(k, v)
  978. }
  979. return
  980. }
  981. func ChineseToPinyin(input string) string {
  982. a := pinyin.NewArgs()
  983. var result strings.Builder
  984. for _, r := range input {
  985. if unicode.Is(unicode.Han, r) {
  986. py := pinyin.Pinyin(string(r), a)
  987. if len(py) > 0 && len(py[0]) > 0 {
  988. result.WriteString(py[0][0])
  989. }
  990. } else if unicode.IsNumber(r) || unicode.IsLetter(r) {
  991. result.WriteRune(r)
  992. }
  993. }
  994. return result.String()
  995. }
  996. func ChineseToPinyinInitials(input string) string {
  997. a := pinyin.NewArgs()
  998. a.Style = pinyin.FirstLetter // 设置风格为首字母
  999. var result strings.Builder
  1000. for _, r := range input {
  1001. if unicode.Is(unicode.Han, r) {
  1002. py := pinyin.Pinyin(string(r), a)
  1003. if len(py) > 0 && len(py[0]) > 0 {
  1004. result.WriteString(py[0][0])
  1005. }
  1006. } else if unicode.IsNumber(r) || unicode.IsLetter(r) {
  1007. result.WriteRune(r)
  1008. }
  1009. }
  1010. return result.String()
  1011. }
  1012. // GetFirstLetter 英文单词首字母
  1013. func GetFirstLetter(name string) string {
  1014. // 定义一个正则表达式,它匹配由字母组成的单词,并捕获每个单词的首字母
  1015. // 注意:这个正则表达式假设单词由字母组成,并且被非字母字符分隔
  1016. // 你可能需要根据实际情况调整这个正则表达式
  1017. re := regexp.MustCompile(`\b[a-zA-Z]`)
  1018. // FindAllString 方法会返回所有匹配的字符串(在这个例子中,是单词的首字母)
  1019. matches := re.FindAllString(name, -1)
  1020. // 将匹配的首字母连接成一个字符串
  1021. newStr := strings.Join(matches, "")
  1022. return strings.ToLower(newStr)
  1023. }
  1024. // 修改供应商信息
  1025. func HttpPostNoCookie(url string, reqParam string, headersParams map[string]string) ([]byte, error) {
  1026. var (
  1027. err error
  1028. )
  1029. // 准备: HTTP请求
  1030. reqBody := strings.NewReader(reqParam)
  1031. httpReq, err := http.NewRequest("POST", url, reqBody)
  1032. if err != nil {
  1033. fmt.Printf("NewRequest fail, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  1034. return nil, err
  1035. }
  1036. for k, v := range headersParams {
  1037. httpReq.Header.Set(k, v)
  1038. }
  1039. //httpReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
  1040. //httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
  1041. client := &http.Client{}
  1042. // DO: HTTP请求
  1043. httpRsp, err := client.Do(httpReq)
  1044. if err != nil {
  1045. fmt.Printf("do http fail, url: %s, reqBody: %s, err:%v", url, reqBody, err)
  1046. return nil, err
  1047. }
  1048. defer httpRsp.Body.Close()
  1049. if httpRsp.StatusCode != 200 {
  1050. err = fmt.Errorf("do http fail, url: %s, statusCode: %d, status:%s ", url, httpRsp.StatusCode, httpRsp.Status)
  1051. return nil, err
  1052. }
  1053. // Read: HTTP结果
  1054. b, err := ioutil.ReadAll(httpRsp.Body)
  1055. if err != nil {
  1056. fmt.Printf("ReadAll failed, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  1057. return nil, err
  1058. }
  1059. // unmarshal: 解析HTTP返回的结果
  1060. // body: {"Result":{"RequestId":"12131","HasError":true,"ResponseItems":{"ErrorMsg":"错误信息"}}}
  1061. return b, err
  1062. }
  1063. func HttpGetNoCookie(url string) ([]byte, error) {
  1064. res, err := http.Get(url)
  1065. if err != nil {
  1066. return nil, err
  1067. }
  1068. defer res.Body.Close()
  1069. return ioutil.ReadAll(res.Body)
  1070. }
  1071. // PostEdbLib 调用指标接口
  1072. func PostEdbLib(param map[string]interface{}, method string) (result []byte, err error) {
  1073. // todo postUrl := EDB_LIB_URL + method
  1074. postUrl := EDB_LIB_URL + method
  1075. postData, err := json.Marshal(param)
  1076. if err != nil {
  1077. return
  1078. }
  1079. result, err = httpPostEtaLib(postUrl, string(postData), "application/json")
  1080. if err != nil {
  1081. return
  1082. }
  1083. return
  1084. }
  1085. type BaseEdbLibResponse struct {
  1086. Ret int
  1087. Msg string
  1088. ErrMsg string
  1089. ErrCode string
  1090. Data interface{}
  1091. }
  1092. func httpPostEtaLib(url, postData string, params ...string) ([]byte, error) {
  1093. fmt.Println("HttpPost Url:" + url)
  1094. body := ioutil.NopCloser(strings.NewReader(postData))
  1095. client := &http.Client{}
  1096. req, err := http.NewRequest("POST", url, body)
  1097. if err != nil {
  1098. return nil, err
  1099. }
  1100. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  1101. if len(params) > 0 && params[0] != "" {
  1102. contentType = params[0]
  1103. }
  1104. req.Header.Set("Content-Type", contentType)
  1105. req.Header.Set("authorization", MD5(APP_EDB_LIB_NAME_EN+EDB_LIB_Md5_KEY))
  1106. //req.Header.Set("authorization", MD5("eta_index_lib"+"2342342341asd"))
  1107. resp, err := client.Do(req)
  1108. if err != nil {
  1109. fmt.Println("client.Do err:" + err.Error())
  1110. return nil, err
  1111. }
  1112. defer resp.Body.Close()
  1113. b, err := ioutil.ReadAll(resp.Body)
  1114. if err != nil {
  1115. fmt.Println("HttpPost:" + string(b))
  1116. }
  1117. return b, err
  1118. }
  1119. func FloatFormatRound(val float64, num int32) float64 {
  1120. val, _ = decimal.NewFromFloat(val).Round(num).Float64()
  1121. return val
  1122. }