JavaScript 最后一个十进制数的增量
JavaScript increment of the the last decimal number
我正在尝试将数字的最后一位小数从 1.234 增加到 1.235
var numb = 1.234;
numb.replace(/\d$/, numb + 1);
或者直接说问题如下
var oNumber = 1.34567
var oDecimalCount = 5
increaseNumber(oNumber, oDecimalCount){
oNumber += //increase the 5th(oDecimalCount) decimal place
}
我不确定我是否完全理解了这个问题,但你不能就这样吗
let numb = 1.234;
numb += 0.001;
你可以这样做:
- 数小数点后的数
- 用这个数字去掉小数点
* 10^n
- 加 1
- 用数字把小数放回原位
/ 10^n
//I found this function here : https://www.tutorialspoint.com/decimal-count-of-a-number-in-javascript
const decimalCount = num => {
// Convert to String
const numStr = String(num);
// String Contains Decimal
if (numStr.includes('.')) {
return numStr.split('.')[1].length;
};
// String Does Not Contain Decimal
return 0;
}
let numb = 1.234;
let count = decimalCount(numb);
console.log(((numb * 10 ** count) + 1) / 10 ** count);
我正在尝试将数字的最后一位小数从 1.234 增加到 1.235
var numb = 1.234;
numb.replace(/\d$/, numb + 1);
或者直接说问题如下
var oNumber = 1.34567
var oDecimalCount = 5
increaseNumber(oNumber, oDecimalCount){
oNumber += //increase the 5th(oDecimalCount) decimal place
}
我不确定我是否完全理解了这个问题,但你不能就这样吗
let numb = 1.234;
numb += 0.001;
你可以这样做:
- 数小数点后的数
- 用这个数字去掉小数点
* 10^n
- 加 1
- 用数字把小数放回原位
/ 10^n
//I found this function here : https://www.tutorialspoint.com/decimal-count-of-a-number-in-javascript
const decimalCount = num => {
// Convert to String
const numStr = String(num);
// String Contains Decimal
if (numStr.includes('.')) {
return numStr.split('.')[1].length;
};
// String Does Not Contain Decimal
return 0;
}
let numb = 1.234;
let count = decimalCount(numb);
console.log(((numb * 10 ** count) + 1) / 10 ** count);