简体   繁体   English

将是十进制数字的JavaScript字符串转换为数字而不会丢失尾随零

[英]Convert JavaScript string which is a decimal number to number without loosing trailing zeros

I am trying to convert a string that is a decimal number to a number without losing the zeros at the end 我正在尝试将十进制数字的字符串转换为数字,而不会在最后丢失零

Convert javaScript floating point numbers to string without loosing insignificant trailing zeros solution here returns a string, not a number 将javaScript浮点数转换为字符串而不丢失不重要的尾随零的解决方案这里返回字符串,而不是数字

let myNumber = '0.10'
parseFloat(myNumber) // 0.1
parseFloat(myNumber).toFixed(2) // "0.10"
Number(parseFloat(myNumber).toFixed(2)) // 0.1

Desired output - convertToNumber('0.10') -> 0.10

I think if you were to create a Number object the only value that it could be is .1. 我认为,如果要创建一个Number对象,则它可能唯一的值为.1。 This is done within the number class, so I do not think you can change this 这是在数字类中完成的,因此我认为您无法更改

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

相关问题 在JavaScript中将数字转换为字符串,而不会在数字后跟零 - Converting number into string in JavaScripts without trailing zeros from number 如何在 JavaScript 中舍入和格式化数字而不使用尾随零? - How to round and format a number in JavaScript without trailing zeros? 如何将 javascript 中的零字符串转换为数字? - How to convert a string with zeros to number in javascript? 确定阶乘 Function 的尾随零的数量(javascript) - Determine Number of Trailing Zeros of a Factorial Function (javascript) JavaScript的最大数字大小与尾随零不同 - JavaScript maximum number size different with trailing zeros Javascript 如何将十进制数转换为具有特定小数位数的字符串 - Javascript How to convert a decimal number to a string with specific number of decimal places 使用JavaScript手动将十进制字符串转换为数字 - Manually Convert A Decimal String To A Number Using JavaScript 将字符串中的负数转换为 JavaScript 中的负十进制数 - Convert negative number in string to negative decimal in JavaScript JavaScript正则表达式:为什么将数字与尾随十进制相匹配? - JavaScript regex: why match number with trailing decimal? JavaScript - 以字符串的形式将十进制数转换为 4 位二进制数 - JavaScript - Convert decimal number to 4-bit binary number in the form of String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM