Created
September 23, 2019 07:02
-
-
Save wangpin34/dacbaa33a6ead19fb8d86d8a2a65bc0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repeatChar(char, times) { | |
if (typeof String.prototype.repeat === 'function') { | |
return char.repeat(times) | |
} | |
let count = 0 | |
const array = [] | |
while (count < times) { | |
count += 1 | |
array.push(char) | |
} | |
return array.join('') | |
} | |
export function genFloat(value, num = 1) { | |
const integer = parseInt(`${value}`, 10) | |
const float = parseFloat(value, 10) - integer | |
if (float === 0) { | |
return ['.', repeatChar('0', num)].join('') | |
} | |
const ratio = Math.pow(10, num) | |
return String(Math.round(float * ratio) / ratio).substr(1) | |
} | |
export function gen(value, num) { | |
const integer = parseInt(value, 10) | |
const float = genFloat(value, num) | |
return `${integer}${float}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment