You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function getLoan(loan){
let Num = Number(loan).toString();
let arr = Num.split(".");
let result = ""
let zs = Number(arr[0]);
let xs = arr[1]
while (zs >= 1000){
result = "," + zs % 1000 + result;
zs = Math.floor(zs/1000)
}
result = zs + result;
if(xs){
result += "."
while(xs.length > 3){
result += xs.slice(0,3) + ","
xs = xs.slice(3);
}
result += xs
}
return result
}
let num = 100000; //下方的转localString可任选一个 let num1 = num.toLocalString('de-DE'); //德国以 . 分割金钱, 转到德国当地格式化方案即可 let num2 = num.toLocalString(); let price = num2.replace(/,/g,'.'); //替换分隔符即可
Activity
thisisandy commentedon Jun 14, 2019
为啥题目描述总这么含糊不清。。。
如果是格式成三位一逗的话
感谢 @SCLeoX 提醒
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
thisisandy commentedon Jun 14, 2019
其他方法还包括
toLocalString
或者使用正则匹配/[0-9](?=(?:[0-9]{3})+(?![0-9]))/g
SCLeoX commentedon Jun 14, 2019
你这个不行啊,significantDigits 设置成 3 的意思是只保留三位数。这个并不是说多少位一逗号的...
haizhilin2013 commentedon Jun 14, 2019
@thisisandy 题目需要你自己审下,正如我们实际工作中,产品经理不可能所有的情况都给你列出来。其实格式化金额的细节还不少,这道题也在于考查分析题的能力,要不然返工的情况就会很多。
例如:
当然你有可能会说这样开发效率会很慢,而且问题很多,不可能全部想全,但是以往的经验告诉我们,如果需求捋得很清楚,编码只不过仅仅是个体力活而已,而且返工率很少,质量就变得很高了。也可以早点下班了!很多的时候我们在修改bug的时间比写代码的时间还多的多,不是吗?
myprelude commentedon Jun 14, 2019
haizhilin2013 commentedon Jun 14, 2019
@myprelude 什么胡任务?不明白
haizhilin2013 commentedon Jun 14, 2019
这个有这么简单吗?
myprelude commentedon Jun 14, 2019
@haizhilin2013 我想到就是这个办法, 胡任务就是我调侃不要在意
haizhilin2013 commentedon Jun 14, 2019
@myprelude 呵呵,我没在意,我只是不知道胡任务是什么意思而已,今天第一听到这个词,所以就特意问问。嘿嘿
myprelude commentedon Jun 14, 2019
@haizhilin2013 老家话大意是:没有之前工作的状态当做每天任务在完成了。形容人做事不认真懒散了,没有责任心了。感觉说的有点过了,我已经删除了。见谅口头禅了
haizhilin2013 commentedon Jun 14, 2019
@myprelude 呵呵,原来是这个意思啊!学习了!没事,放着就好,又get到新知识了,非常感谢!这些题都很好的,你们答可能就几分钟,我出一道题可能要花上个10多分钟想……嘿嘿,加油!看似越简单的题包含的信息量就越大
haizhilin2013 commentedon Jun 14, 2019
@myprelude 这道题有时间可以在好好想想!你直接就用toLocaleString是不行的!
yxkhaha commentedon Jun 14, 2019
xn213 commentedon Jun 14, 2019
toFixed() 浮点数陷阱 怎么处理的
Cillivian commentedon Jul 26, 2019
一直都是使用tolocalestring
jiamianmao commentedon Aug 11, 2019
这个case很常见,
https://ant.design/components/input-number-cn/
通常在中后台项目中使用这个。这个需要考虑的情况确实太多了。
laixihong commentedon Dec 24, 2019
rni-l commentedon Jan 19, 2020
276378532 commentedon Aug 21, 2020
wepn13232 commentedon Dec 14, 2020
如果是3位数字转逗号那种的话
let num = 100000;
//下方的转localString可任选一个
let num1 = num.toLocalString('de-DE'); //德国以 . 分割金钱, 转到德国当地格式化方案即可
let num2 = num.toLocalString();
let price = num2.replace(/,/g,'.'); //替换分隔符即可
smile-2008 commentedon Dec 15, 2020
wind8866 commentedon Mar 3, 2022
xiaoqiangz commentedon Jun 10, 2022
toLocaleString