用户:
卑微OIer查看:1 回复:15 评论:1 创建时间:2022-04-01T11:11:19
------------------------------------------------------------------------------
老师:xdm读一下114514114514
同学:(心里t.m的,怎么长让我则么读?)
老师:1145.141114514亿啊
同学:太长了,四舍五入1145.14亿
所以本教程的内容是如何将这臭数字转化一下
Step1 个十百千万
在百度上搜索一下数字单位,我们立马就找到了
const digital = ['','万','亿','兆','京','垓','秭','穰','沟','涧','正','载','极',
'恒河沙','阿僧之','那由他','不可思议','无量','大数','全仕祥','古戈尔'];
Step2 写代码
前提了解 log函数
const logs=Math.log10(number);
const behind = Math.floor(logs/4);
let fronts = number/Math.pow(10,behind*4);
fronts = Math.floor(fronts*1000)/1000;
console.log(fronts+digital[behind]);
封装函数)
console.clear();
function turnDigit(number){
const digital = ['','万','亿','兆','京','垓','秭','穰','沟','涧','正','载','极','恒河沙','阿僧之','那由他','不可思议','无量','大数','全仕祥','古戈尔'];
const logs=Math.log10(number);
const behind = Math.floor(logs/4);
let fronts = number/Math.pow(10,behind*4);
fronts = Math.floor(fronts*1000)/1000;
return (fronts+digital[behind]);
}
效果
123 => 123
114514 => 11.45万