Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Created September 23, 2019 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangpin34/dacbaa33a6ead19fb8d86d8a2a65bc0b to your computer and use it in GitHub Desktop.
Save wangpin34/dacbaa33a6ead19fb8d86d8a2a65bc0b to your computer and use it in GitHub Desktop.
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