Last active
August 29, 2015 13:59
-
-
Save slikts/10471861 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
var pattern = /^(\d+?)(0+)$/; | |
function ne(n) { | |
'use strict'; | |
var str = String(n); | |
var match = str.match(pattern); | |
var len; | |
if (!match) { | |
return str; | |
} | |
len = match[2].length; | |
if (len > 3) { | |
if (match[1] === '1') { | |
match[1] += '0'; | |
len -= 1; | |
} | |
str = match[1] + 'e' + len; | |
} | |
return str; | |
}; | |
console.log(ne(10e1)); // "100" | |
console.log(ne(10e3)); // "1000" | |
console.log(ne(10e4)); // "10e4" etc. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment