简体   繁体   English

如何获得 TypeScript 中数字的总位数?

[英]How can I get a count of the total number of digits in a number in TypeScript?

Declaration of a number variable called temp in TypeScript:在 TypeScript 中声明一个名为temp的数字变量:

temp: number = 0.123;

How can I get a count of the total number of digits in a number (which is 3)?我怎样才能得到一个数字的总位数(即 3)?

If I understood correct, you want the part after .如果我理解正确,您需要. . .

You can parse it into a string, split by .你可以解析成一个字符串, split. and count the string length of the second item.并计算第二项的字符串长度。

 const temp = 0.123; const tempArray = temp.toString().split('.'); const length = tempArray[1].length; console.log(length);

in typescript simply use:在打字稿中只需使用:

this.number.toString().split('.'))[1].length this.number.toString().split('.'))[1].length

Where this.number is Type of Number this.number 是数字类型

Use this Regexp \\.\\d+使用这个正则表达式\\.\\d+

 var temp = 0.123; var regex = new RegExp('\\\\.\\\\d+', 'g'); console.log(temp.toString().match(regex).pop().length - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM