Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active August 29, 2015 13:59
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 slikts/10471861 to your computer and use it in GitHub Desktop.
Save slikts/10471861 to your computer and use it in GitHub Desktop.
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