jquery formatCurrency货币格式化处理
// This file is part of the jQuery formatCurrency Plugin.
//
// The jQuery formatCurrency Plugin is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // The jQuery formatCurrency Plugin is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// the jQuery formatCurrency Plugin. If not, see <http://www.gnu.org/licenses/>.
// wiki http://code.google.com/p/jquery-formatcurrency/wiki/Usage (function($) { $.formatCurrency = {}; $.formatCurrency.regions = []; // default Region is en
$.formatCurrency.regions[''] = {
symbol: '',
positiveFormat: '%s%n',
negativeFormat: '(%s%n)',
decimalSymbol: '.',
digitGroupSymbol: ',',
groupDigits: true
}; $.fn.formatCurrency = function(destination, settings) { if (arguments.length == 1 && typeof destination !== "string") {
settings = destination;
destination = false;
} // initialize defaults
var defaults = {
name: "formatCurrency",
colorize: false,
region: '',
global: true,
roundToDecimalPlace: 2, // roundToDecimalPlace: -1; for no rounding; 0 to round to the dollar; 1 for one digit cents; 2 for two digit cents; 3 for three digit cents; ...
eventOnDecimalsEntered: false
};
// initialize default region
defaults = $.extend(defaults, $.formatCurrency.regions['']);
// override defaults with settings passed in
settings = $.extend(defaults, settings); // check for region setting
if (settings.region.length > 0) {
settings = $.extend(settings, getRegionOrCulture(settings.region));
}
settings.regex = generateRegex(settings); return this.each(function() {
$this = $(this); // get number
var num = '0';
num = $this[$this.is('input, select, textarea') ? 'val' : 'html'](); //identify '(123)' as a negative number
if (num.search('\\(') >= 0) {
num = '-' + num;
} if (num === '' || (num === '-' && settings.roundToDecimalPlace === -1)) {
return;
} // if the number is valid use it, otherwise clean it
if (isNaN(num)) {
// clean number
num = num.replace(settings.regex, ''); if (num === '' || (num === '-' && settings.roundToDecimalPlace === -1)) {
return;
} if (settings.decimalSymbol != '.') {
num = num.replace(settings.decimalSymbol, '.'); // reset to US decimal for arithmetic
}
if (isNaN(num)) {
num = '0';
}
} // evalutate number input
var numParts = String(num).split('.');
var isPositive = (num == Math.abs(num));
var hasDecimals = (numParts.length > 1);
var decimals = (hasDecimals ? numParts[1].toString() : '0');
var originalDecimals = decimals; // format number
num = Math.abs(numParts[0]);
num = isNaN(num) ? 0 : num;
if (settings.roundToDecimalPlace >= 0) {
decimals = parseFloat('1.' + decimals); // prepend "0."; (IE does NOT round 0.50.toFixed(0) up, but (1+0.50).toFixed(0)-1
decimals = decimals.toFixed(settings.roundToDecimalPlace); // round
if (decimals.substring(0, 1) == '2') {
num = Number(num) + 1;
}
decimals = decimals.substring(2); // remove "0."
}
num = String(num); if (settings.groupDigits) {
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
num = num.substring(0, num.length - (4 * i + 3)) + settings.digitGroupSymbol + num.substring(num.length - (4 * i + 3));
}
} if ((hasDecimals && settings.roundToDecimalPlace == -1) || settings.roundToDecimalPlace > 0) {
num += settings.decimalSymbol + decimals;
} // format symbol/negative
var format = isPositive ? settings.positiveFormat : settings.negativeFormat;
var money = format.replace(/%s/g, settings.symbol);
money = money.replace(/%n/g, num); // setup destination
var $destination = $([]);
if (!destination) {
$destination = $this;
} else {
$destination = $(destination);
}
// set destination
$destination[$destination.is('input, select, textarea') ? 'val' : 'html'](money); if (
hasDecimals &&
settings.eventOnDecimalsEntered &&
originalDecimals.length > settings.roundToDecimalPlace
) {
$destination.trigger('decimalsEntered', originalDecimals);
} // colorize
if (settings.colorize) {
$destination.css('color', isPositive ? 'black' : 'red');
}
});
}; // Remove all non numbers from text
$.fn.toNumber = function(settings) {
var defaults = $.extend({
name: "toNumber",
region: '',
global: true
}, $.formatCurrency.regions['']); settings = jQuery.extend(defaults, settings);
if (settings.region.length > 0) {
settings = $.extend(settings, getRegionOrCulture(settings.region));
}
settings.regex = generateRegex(settings); return this.each(function() {
var method = $(this).is('input, select, textarea') ? 'val' : 'html';
$(this)[method]($(this)[method]().replace('(', '(-').replace(settings.regex, ''));
});
}; // returns the value from the first element as a number
$.fn.asNumber = function(settings) {
var defaults = $.extend({
name: "asNumber",
region: '',
parse: true,
parseType: 'Float',
global: true
}, $.formatCurrency.regions['']);
settings = jQuery.extend(defaults, settings);
if (settings.region.length > 0) {
settings = $.extend(settings, getRegionOrCulture(settings.region));
}
settings.regex = generateRegex(settings);
settings.parseType = validateParseType(settings.parseType); var method = $(this).is('input, select, textarea') ? 'val' : 'html';
var num = $(this)[method]();
num = num ? num : "";
num = num.replace('(', '(-');
num = num.replace(settings.regex, '');
if (!settings.parse) {
return num;
} if (num.length == 0) {
num = '0';
} if (settings.decimalSymbol != '.') {
num = num.replace(settings.decimalSymbol, '.'); // reset to US decimal for arthmetic
} return window['parse' + settings.parseType](num);
}; function getRegionOrCulture(region) {
var regionInfo = $.formatCurrency.regions[region];
if (regionInfo) {
return regionInfo;
}
else {
if (/(\w+)-(\w+)/g.test(region)) {
var culture = region.replace(/(\w+)-(\w+)/g, "$1");
return $.formatCurrency.regions[culture];
}
}
// fallback to extend(null) (i.e. nothing)
return null;
} function validateParseType(parseType) {
switch (parseType.toLowerCase()) {
case 'int':
return 'Int';
case 'float':
return 'Float';
default:
throw 'invalid parseType';
}
} function generateRegex(settings) {
if (settings.symbol === '') {
return new RegExp("[^\\d" + settings.decimalSymbol + "-]", "g");
}
else {
var symbol = settings.symbol.replace('$', '\\$').replace('.', '\\.');
return new RegExp(symbol + "|[^\\d" + settings.decimalSymbol + "-]", "g");
}
} })(jQuery);
1.引入jquery和插件(jquery省略)
<script src="jquery.formatCurrency-1.4.0.js" type="text/javascript" ></script>
2.使用插件API方法
// 如将页面所有表格的金额单元格格式化显示
$('.label').formatCurrency(); //
$('.ageInput').toNumber();
jquery formatCurrency货币格式化处理的更多相关文章
- Java中货币格式化
		
private final static NumberFormat CURRENCY_FORMAT = NumberFormat.getCurrencyInstance(Locale.CHINA); ...
 - [SAP ABAP开发技术总结]数据输入输出转换、小数位/单位/货币格式化
		
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
 - jquery easyUI 日期格式化,DateBox只显示年
		
jquery easyUI 日期格式化,DateBox只显示年 >>>>>>>>>>>>>>>>> ...
 - vue货币格式化组件、局部过滤功能以及全局过滤功能
		
一.在这里介绍一个vue的时间格式化插件: moment 使用方法: .npm install moment --save. 2 定义时间格式化全局过滤器 在main.js中 导入组件 import ...
 - 转:前端js、jQuery实现日期格式化、字符串格式化
		
1. js仿后台的字符串的StringFormat方法 在做前端页面时候,经常会对字符串进行拼接处理,但是直接使用字符串拼接,不但影响阅读,而且影响执行效率,且jQuery有没有定义字符串的Strin ...
 - 前端js、jQuery实现日期格式化、字符串格式化
		
1. js仿后台的字符串的StringFormat方法 在做前端页面时候,经常会对字符串进行拼接处理,但是直接使用字符串拼接,不但影响阅读,而且影响执行效率,且jQuery有没有定义字符串的Strin ...
 - JS对数字进行货币格式化并且保留两位小数点,小数用0补全
		
/** * 将数值四舍五入(保留2位小数)后格式化成金额形式 * * @param num 数值(Number或者String) * @return 金额格式的字符串,如'1,234,567.45' ...
 - thymeleaf 货币格式化  数字格式化问题
		
格式化数字对象 ${'¥'+#numbers.formatDecimal(pro.price,0,'COMMA',2,'POINT')} ${'¥'+#numbers.formatDecimal(pr ...
 - JSTL fmt:formatNumber 数字、货币格式化
		
<fmt:formatNumber value="12.34" pattern="#0.00" /> 12.34 保留小数点后两位数 <fmt ...
 
随机推荐
- FileInputStream 与 BufferedInputStream 效率对比
			
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3550158.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...
 - php 中 isset()函数 和 empty()函数的区别
			
首先这两个函数都是用来测试变量的状态: isset()函数判断一个变量是否在 如果存在返回true 否则返回false empty()函数判断一个变量是否为空,如果为空返回true 否则返回fals ...
 - Linux中的时间和时间管理
			
Coordinated Universal Time(UTC):协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT).比如,中国内地的时 ...
 - centos 安装php ide (eclipse + php 插件)
			
1.检查更新并安装eclipse yum check-update yum install eclipse*此时的 eclipse 已经安装好了,默认是在/usr/lib/下的,可以通过cd /u ...
 - html分页
			
<div class="fy"> <a href="" title="上一页">上一页</a> < ...
 - c#个人记录常用方法(更新中)
			
1.日期毫秒转换为标准的C#日期格式 //使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33 public DateTime JavaTimeToC(long ...
 - CSS3 animation-fill-mode 属性
			
现在专注于移动端开发项目,对于动画这个点是非常重要的,每当我遇到一个新的知识点,我就会和大家一起分享 animation-fill-mode :把物体动画地从一个地方移动到另一个地方,并让它停留在那里 ...
 - 制作Mac OS X Mavericks 安装U盘
			
1. 8G+ U盘一个. 2. App Store 下载Maverics到本地(默认会下载到Applications) 2. 打开Mac OS 磁盘工具(Disk Utility),左侧选中U盘,在右 ...
 - WPF中三种方法得到当前屏幕的宽和高
			
WPF程序中的单位是与设备无关的单位,每个单位是1/96英寸,如果电脑的DPI设置为96(每个英寸96个像素),那么此时每个WPF单位对应一个像素,不过如果电脑的DPI设备为120(每个英寸120个像 ...
 - 把Mvc4项目部署到虚拟目录之后找不到control想到的文件路径规范的问题
			
最近部署的项目的时候由于端口不够用,想到了把Mvc项目部署到虚拟目录中,结果发现图片,js设置control都找不到了.项目是mvc4+easyui开发的,大量的代码都是在js中调用control,写 ...