JSFiddle - React, Tailwind, and code Playground
by crucify
HTML
입력:<input type="text" id="in" /><br />
numberFormat():<input type="text" id="out" /><br />
humanFormat():<input type="text" id="out2" /><br />
strpad():<input type="text" id="out3" />
JavaScript
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------+
// | Copyright : Song Hyo-Jin <shj at xenosi.de> |
// +--------------------------------------------------------+
// | License : BSD |
// +--------------------------------------------------------+
//
// $Id: number_formatting.js, 2012. 4. 6. crucify Exp $
String.prototype.toInt = function() {
var pm = /^-/.test(this) ? -1 : 1;
return this.replace(/\..*$/g, '').replace(/[^\d]/g, '') * pm;
}
String.prototype.toNum = function() {
var pm = /^-/.test(this) ? -1 : 1;
return this.replace(/(\.[^\.]+)\..*$/g, '$1').replace(/[^\d\.]/g, '') * pm;
}
String.prototype.reverse = function() {
return this.match(/./g).reverse().join('');
}
String.prototype.numberFormat = function() {
var num = (this.toNum() + '').split(/\./);
var res = [];
res.push(num[0].reverse().replace(/(\d{3})(?=\d)/g, '$1,').reverse());
if(num.length > 1) res.push(num[1].replace(/(\d{3})(?=\d)/g, '$1,'));
return res.join('.');
}
Number.prototype.numberFormat = function() {
return (this + '').numberFormat();
}
String.prototype.humanFormat = function() {
if(this == '' || this == '0') return 0;
return this.toNum().humanFormat();
}
Number.prototype.humanFormat = function(u) {
if(this == 0) return 0;
if(!u) u = 1000;
var units = ['', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'];
var idx = Math.min(Math.floor(Math.log(this) / Math.log(u)), units.length - 1);
if(idx == 0) return this;
return (Math.ceil(this / Math.pow(u, idx) * 100) / 100) + units[idx];
}
Number.prototype.strpad = function(c, f) {
return (this + '').strpad(c, f);
}
String.prototype.strpad = function(c, f) {
if(!f && f != 0) f = '0';
f = f + '';
var res = this;
while(res.length < c) res = f + res;
return res;
}
$(function() {
$('#in').keyup(function() {
...