wNumb Formatting options Download wNumb

wNumb.js

JavaScript Number & Money formatting

wNumb is a formatting library with a dead-simple interface. It has two methods: to and from.

Licensed MIT, so free for personal and commercial use.
At 989 bytes for the compressed + Gzipped version, page loads won't suffer.

Works in all modern browsers and IE7+.

Download wNumb from GitHub All formatting options

Usage

var moneyFormat = wNumb({
	mark: '.',
	thousand: ',',
	prefix: '$ ',
	suffix: ' p.p.'
});

// Format a number:
moneyFormat.to( 301980.62 );
	=> '$ 301,980.62 p.p.'

// Get a number back:
moneyFormat.from( '$ 301,980.62 p.p.' );
	=> 301980.62

Examples

// Appending money-formatting
var Format = wNumb({
	prefix: '$ ',
	decimals: 3,
	thousand: ','
});

// Numbers are always correctly rounded
Format.to ( 980735.2635 )
	=> '$ 980,735.264'
// Manually edit numbers
var Format = wNumb({
	thousand: '.',
	encoder: function( a ){
		return a * 1E7;
	},
	decoder: function( a ){
		return a / 1E7;
	}
});

Format.from ( '-95.060.000.000' )
	=> -9506
// Prefixing negative numbers with a string
var Format = wNumb({
	prefix: '$',
	negativeBefore: '[NEGATIVE] '
});

Format.to ( -260 )
	=> '[NEGATIVE] $260'
Format.from ( '[NEGATIVE] $260' )
	=> -260
// Parsing miss-matches and typo's
var Format = wNumb({
	prefix: '$',
	suffix: ',-',
	thousand: ','
});

Format.to ( '90000' )
	=> '$90,000,-'

// No problem. If there is a sensible number in the entry,
// it'll be extracted, even if pre/post-fixes are missing or off.
Format.from ( '90000 money' )
	=> 90000

// No idea... If the value makes no sense,
// the 'from' method returns false.
Format.from ( 'spaaaaacceeee!!!' )
	=> false