I've been trying to figure out how to add period as a string in a certain place, here's the code
function fixBalance(amount){
if(amount <= 99999999){
var amt = "0." amount.toString().padStart(8, "0");
return Number(amt);
} else {
return false;
}
}
console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));
Basically, there are 8 spaces to the right of the 0
After it passes 8 numbers, I want numbers to keep going past the period
For example:
1 = 0.00000001
1000 = 0.00001000 or 0.00001
10000000 = 0.10000000 or 0.1
100001000 = 0.100001000
1000000001 = 10.00000001
1010000000 = 10.10000000
10100000000 = 101.00000000
If I were to put in 1000000000, it should be 10.00000000 or 10 as a numerical number
Trying to figure out how to do that with the else statement
Thank you!
CodePudding user response:
How about determining the integer and decimal parts separately before composing them?
function fixBalance(amount){
const divisor = 100_000_000;
const integer = Math.floor(amount / divisor);
const decimal = amount % divisor;
return integer.toString() "." decimal.toString().padStart(8, "0");
}
console.log(fixBalance(1));
console.log(fixBalance(1000));
console.log(fixBalance(10000000));
console.log(fixBalance(1000000000));