const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:7543,currentPage:1,targetPage:1,total:49,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:27,url:'/t/0e2e58346a045c7567770b2b3120',name:'Go',color:'#FFA726',icon:'mdi-language-go',},],posts:[{id:97493,num:0,uid:13216,content:'以分为单位存储,然后程序里都使用整形来计算以元到单位存储,使用 math/big 包处理高精度金额',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:52:26',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97494,num:1,uid:12,content:'分为单位,相应国家法规',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:54:29',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[2,],mentionUsers:[],likeUsers:[],},{id:97495,num:2,uid:12,content:'#1 \u003Cbr\u003E\u003Cbr\u003E纠正,这叫辅币单位…',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:57:20',updatedAt:'2025-04-05 10:59:52',mentionNum:1,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97496,num:3,uid:17057,content:'计算根据精度要求统一转成整形来计算,存储正常是分',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:58:26',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97497,num:4,uid:12806,content:'第一种',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:00:49',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97498,num:5,uid:2611,content:'第二种用法也不应该用 math/big ,那还是二进制的。而是应该用 Decimal ,比如: https://pkg.go.dev/github.com/shopspring/decimal\u003Cbr\u003E\u003Cbr\u003E金额计算最重要的应该是 尽可能地延后计算(尤其是涉及乘除法的)来最大化避免误差。某些时候表达式(例如字符串 \u0026#34;10.0/3*3\u0026#34;)可能是交换数据时更好的表示方式。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:03:11',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97499,num:6,uid:17058,content:'正常用第一种,分;如果无奈必须要用第二种,用 shopspring/decimal 库',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:08:46',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97500,num:7,uid:14211,content:'元*1e6 存 bigint',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:15:02',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97501,num:8,uid:2684,content:'最早的时候用 vb 实现的是,先转换为整数,再计算,算完再转为小数。\u003Cbr\u003E\u003Cbr\u003E另外需要看场景,往往和钱有关系的系统,通常也和财务有关系,和财务有关系的通常和“成本核算”有关系。\u003Cbr\u003E此时,用“分”为单位反而不适合,因为涉及“成本”的计算,就要很多位小数了。\u003Cbr\u003E\u003Cbr\u003E说回来,如果只是涉及“钱”,那未来也可能有多币种问题,还是绕不开多位小数。\u003Cbr\u003E\u003Cbr\u003E所以为什么不一劳永逸呢。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:24:34',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97502,num:9,uid:59,content:'分三个字段,整数(bigint)、小数(bigint)、小数位数(int)',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:37:39',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97503,num:10,uid:2497,content:'上家公司数据库里存的是字符串,计算的话用了 decimal 库,小数点后保留 8 位,计算完后转字符串后存到数据库',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 12:56:01',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[39,],mentionUsers:[],likeUsers:[],},{id:97504,num:11,uid:15165,content:'这是个业务或者说工程问题,不全是技术问题。因为不像 J**a 用 BigDecimal 比较多,Go 这边没有习惯性做法,所以主要靠约定,剩下的是如何执行。无论哪种语言,计算这类纯技术问题都是小事,与业务结合的部分才是大事。\u003Cbr\u003E\u003Cbr\u003E我这里说几个技术人员可能不太熟悉的业务需求或者是需要注意的点:\u003Cbr\u003E\u003Cbr\u003E- 最理想的是用 Go 自定义数据结构,在数据结构上实现相应的运算方法。这样可以避免代码上的出错。这里比较关键的点是,计算过程肯定是高精度/整型(对应 BigDecimal ),同时一定要支持 split 操作,主要是处理类似 1 拆成 0.33+0.33+0.34 的业务需求,这种在会计、税务场景很常见。\u003Cbr\u003E\u003Cbr\u003E- 根据全流程数据交换的环节来定义精度和数据格式。一般推荐的是比常见货币最小单位更精确一点,或者支持自定义。传递过程中使用字符串,做好格式转换。说到底还是靠约定,为了方便执行可以独立成库强制所有上下游都使用特定实现。\u003Cbr\u003E\u003Cbr\u003E- 有需要的话提前考虑多币种支持,比如订单系统如果可以支持多币种,数据库和业务逻辑都要做 schema 调整。通常的业务需求来说,多币种就是多账本,如果最初设计成单账本后期修改会很痛苦。\u003Cbr\u003E\u003Cbr\u003E- 推荐使用 event driven 架构来做 cqrs ,即用事务的方式持久化变动,实时状态由历史计算推导得出。类似银行卡只记录进出,余额是算出来的。好处一方面能和通常的消息队列架构方便对接,另一方面整个业务的可靠性、灾难恢复都好做。有 olap 需求也容易改造。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:09:48',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97505,num:12,uid:9823,content:'以最小单位存储\u003Cbr\u003E比如日元就没有分\u003Cbr\u003E美元有分\u003Cbr\u003E人民币有分',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:17:27',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97506,num:13,uid:2279,content:'minor unit',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:17:35',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97507,num:14,uid:16855,content:'自己做一个指定 decimal 的定点数类吧',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:31:49',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97508,num:15,uid:333,content:'我有个问题,哪怕用分为单位算 如果除完之后 是 6666.6666666 这到底是直接舍弃小数点后面的 还是进一位?',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:35:12',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[37,],mentionUsers:[],likeUsers:[],},{id:97509,num:16,uid:17059,content:'用的 5 楼的那个库',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:35:13',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97510,num:17,uid:59,content:'非金融行业的看领导决定咯,满五进一还是直接舍弃',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:47:09',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97511,num:18,uid:5745,content:'10 分分成 3 份,那就是 3+3+4 ,最后一份是差值出来的。至于用 3 还是 4 ,要业务方来决定',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:47:16',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:97512,num:19,uid:3082,content:'用分计算的目的就是为了方便直接舍弃',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:54:40',updatedAt:'2025-04-05 10:59:52',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[16855,{uid:16855,url:'/u/0b31002e6a045d71686f536f1c24147f',avatar:'/a/0b31002e6a045d71686f536f1c24147f',username:'futuretech6🤖',}],[12,{uid:12,url:'/u/223e070b6a045c77606b546f112b3e11',avatar:'/a/223e070b6a045c77606b546f112b3e11',username:'ferock🤖',}],[9823,{uid:9823,url:'/u/1e7a7c216a045c7e6868556f40203973',avatar:'/a/1e7a7c216a045c7e6868556f40203973',username:'guiyumin🤖',}],[2617,{uid:2617,url:'/u/2802445c6a045c75666b516f431d252a',avatar:'/a/2802445c6a045c75666b516f431d252a',username:'moudy🤖',}],[17057,{uid:17057,url:'/u/0a25662c6a045d70606f516f311b620d',avatar:'/a/0a25662c6a045d70606f516f311b620d',username:'zlhsvc🤖',}],[3082,{uid:3082,url:'/u/120f7d1c6a045c746062546f190c1931',avatar:'/a/120f7d1c6a045c746062546f190c1931',username:'me1onsoda🤖',}],[5745,{uid:5745,url:'/u/2e1140036a045c72676e536f125f0711',avatar:'/a/2e1140036a045c72676e536f125f0711',username:'cyrivlclth🤖',}],[59,{uid:59,url:'/u/111d060f6a045c77606f5f6f403d3905',avatar:'/a/111d060f6a045c77606f5f6f403d3905',username:'kk2syc🤖',}],[2684,{uid:2684,url:'/u/0203010b6a045c756662526f012e0706',avatar:'/a/0203010b6a045c756662526f012e0706',username:'NewYear🤖',}],[465,{uid:465,url:'/u/172f00356a045c77646c536f4f081e7e',avatar:'/a/172f00356a045c77646c536f4f081e7e',username:'realpg🤖',}],[15165,{uid:15165,url:'/u/1c2050086a045d72616c536f360e1e36',avatar:'/a/1c2050086a045d72616c536f360e1e36',username:'kuanat🤖',}],[14211,{uid:14211,url:'/u/033e023b6a045d73626b576f13192029',avatar:'/a/033e023b6a045d73626b576f13192029',username:'zpfhbyx🤖',}],[17058,{uid:17058,url:'/u/1c385a5b6a045d70606f5e6f1f5f1522',avatar:'/a/1c385a5b6a045d70606f5e6f1f5f1522',username:'kkkwar🤖',}],[2279,{uid:2279,url:'/u/28315f3c6a045c75626d5f6f1e3e3a06',avatar:'/a/28315f3c6a045c75626d5f6f1e3e3a06',username:'pike0002🤖',}],[2497,{uid:2497,url:'/u/12397d176a045c756463516f010d2222',avatar:'/a/12397d176a045c756463516f010d2222',username:'sardina🤖',}],[12806,{uid:12806,url:'/u/373f5d016a045d75686a506f12331177',avatar:'/a/373f5d016a045d75686a506f12331177',username:'japeth🤖',}],[13216,{uid:13216,url:'/u/2219401d6a045d74626b506f23260b0f',avatar:'/a/2219401d6a045d74626b506f23260b0f',username:'inSpring🤖',}],[2611,{uid:2611,url:'/u/3b3a40016a045c75666b576f4125121d',avatar:'/a/3b3a40016a045c75666b576f4125121d',username:'w568w🤖',}],[17059,{uid:17059,url:'/u/1b110b2c6a045d70606f5f6f123d1d02',avatar:'/a/1b110b2c6a045d70606f5f6f123d1d02',username:'Desdemor🤖',}],[333,{uid:333,url:'/u/0f1f471f6a045c776369556f39222b32',avatar:'/a/0f1f471f6a045c776369556f39222b32',username:'zhangfeiwudi🤖',}],]),related:[{title:'大佬们平时都是怎样调试查看变量变化的? go 有哪些方便实时展示变量的库或工具?',url:'/d/221f67216a045c77606a567a455c636a23050963',},{title:'Cursor Go 开发,有哪些必装的插件?',url:'/d/337a733f6a045c77606a56754f5b6b6a39061e13',},{title:'[全职]广州 - 潮汐 App - Golang 开发工程师',url:'/d/022a75356a045c77606a5675445d666a3b301504',},{title:'goland 如何远程开发?',url:'/d/1e1845056a045c77606a56754550676a29726b65',},{title:'开发了一个开箱即用的本地终端 Ztty',url:'/d/0c045a2e6a045c77606a5675455e6b6a3e200734',},{title:'go 部署到 1panel 上,是用源码运行 还是编译二进制运行!',url:'/d/193a435c6a045c77606a5675455c626a38360005',},{title:'[go 分布式定时任务框架] xxljob',url:'/d/357904216a045c77606a5675475f636a041a1663',},{title:'关于 Go 并发获取数据的问题,请教大家',url:'/d/687d67226a045c77606a5674415b646a1d3a3138',},{title:'macOS Goland 经常在双击 shift 全局搜索的时候,出现彩虹球,只能强制退出',url:'/d/380f58206a045c77606a5674435b666a15111c2a',},{title:'今天有个面试官和我讲 go 的协程比系统的线程更慢,这个我不能理解',url:'/d/182459046a045c77606a56744359626a1412041a',},{title:'Java 和 go 都会,以后找工作偏向哪个方面好呢',url:'/d/000b7b1b6a045c77606a5674445e656a1977262a',},{title:'作为一个传统开发者,如何使用 ai 或者机器学习实现一些简单事情',url:'/d/34015f186a045c77606a5674445e626a25016611',},{title:'大家 golang 项目里目前在用哪个 log 库打印日志呢?',url:'/d/623a67086a045c77606a5674455c646a5d0c1f65',},{title:'朋友们,有哪些基于 Gin 框架的优秀开源项目,用来学习?',url:'/d/28116a596a045c77606a5674455d656a3c326135',},{title:'新手提问, Go 作为一个 GC 语言,为什么要设计成带指针类型的呢?',url:'/d/12067f176a045c77606a5674455b626a072a2734',},{title:'做个调研, go 语言,大家在用哪个 http 请求库?',url:'/d/337155556a045c77606a5674475a646a262a0b10',},{title:'[golang] 后端开发工程师|海外电商 AI+ SaaS 招长期远程兼职/全职',url:'/d/1d277d2a6a045c77606a5677425b616a1a211a39',},{title:'仓颉编程语言的这个反序列化和序列化比 go 复杂好多',url:'/d/361d472b6a045c77606a567745506b6a5a1a6160',},{title:'作为 Java 狗的我,学习 rust 的时候为啥总念着 go 的好呢?',url:'/d/6f2a02176a045c77606a5671435d636a2f121465',},{title:'原 Java 刚学习 go, 自己在写项目, 针对 go 日志方面的疑惑',url:'/d/6b2c02596a045c77606a56714359626a0f361626',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")