Jquery; Number to currency
Im currently working on a little app and i need the following to happen;
The number 100000 should be changed to 100.000.
I tried toFixed(), but that just ad开发者_开发百科ds numbers and i tried a few other things without success. Anyone has an idea how to fix this?
Try this:
(yourNumber / 1000).toFixed(3)
Check out the jQuery Format Currency Plugin
Try this:
yourNumber/1000
I hope this is what you needed?
A project seems to exist to resolve your very problem: http://code.google.com/p/jquery-formatcurrency/ .
A quick usage demonstration:
$('#id_of_span_containing_value').formatCurrency({digitGroupSymbol: '.', decimalSymbol: ','});
It seems to focus on formatting page elements, and not strings themselves, even though it seems logical since currency formatting is a presentation problem, and is nicely solved this way.
I wrote a small function to do this a while ago. Works on negative numbers and decimals too:
// Only perform the formatting if we haven't already
if(currency.indexOf(',') === -1) {
var removedString = '';
if(currency.indexOf('-') > -1) {
currency = currency.replace('-', '');
removedString = '-';
}
var decimal = currency.indexOf('.');
decimal = decimal > 0 ? decimal : currency.length;
for(var i = decimal - 3; i>0; i=i-3) {
currency = currency.slice(0, i) + ',' + currency.slice(i, currency.length);
}
return removedString + current;
}
Edit: This is for native JavaScript, jQuery may have formatting shortcuts. It's also localized to GBP, so you may have to change the commas to decimal points if that is your localization.
Use the autoNumeric plugin of jquery plugin that automatically formats currency and numbers as you type on form inputs
First - include jQuery.js and autoNumeric-1.9.19.js javascript files in the header:
Second - insert a form and input field on the HTML/JSP document:
Third - in a separate script initialize autoNumeric $('selector').autoNumeric('init'):
jQuery(function($) {
$('#someID_defaults').autoNumeric('init', {aSign:',', pSign:'£', vMax:'99999.99' });
});
This has nothing to do with jQuery and Javascript does not natively implement this. Take a look at: http://ntt.cc/2008/04/25/6-very-basic-but-very-useful-javascript-number-format-functions-for-web-developers.html
EDITOR NOTE: The link no longer works
精彩评论