格式化是通过格式操作使任意类型的数据转换成一个字符串。例如下面这样
<script>
console.log(chopper.format('{0} - {1} - {2}', 12, 24, 25)); // outputs "12 - 24 - 25"
</script>
下面是一个完整的代码,可以复制到自己的项目中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 5 </head> 6 <body> 7 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 8 <script> 9 (function() { 10 var chopper = window.chopper = window.chopper || { cultures: {} }, 11 math = Math, 12 formatRegExp = //{(/d+)(:[^/}]+)?/}/g, 13 FUNCTION = "function", 14 STRING = "string", 15 NUMBER = "number", 16 OBJECT = "object", 17 NULL = "null", 18 BOOLEAN = "boolean", 19 UNDEFINED = "undefined", 20 slice = [].slice, 21 globalize = window.Globalize, 22 standardFormatRegExp = /^(n|c|p|e)(/d*)$/i, 23 literalRegExp = /(//.)|(['][^']*[']?)|(["][^"]*["]?)/g, 24 commaRegExp = //,/g, 25 EMPTY = "", 26 POINT = ".", 27 COMMA = ",", 28 SHARP = "#", 29 ZERO = "0", 30 PLACEHOLDER = "??", 31 EN = "en-US", 32 objectToString = {}.toString; 33 34 //cultures 35 chopper.cultures["en-US"] = { 36 name: EN, 37 numberFormat: { 38 pattern: ["-n"], 39 decimals: 2, 40 ",": ",", 41 ".": ".", 42 groupSize: [3], 43 percent: { 44 pattern: ["-n %", "n %"], 45 decimals: 2, 46 ",": ",", 47 ".": ".", 48 groupSize: [3], 49 symbol: "%" 50 }, 51 currency: { 52 pattern: ["($n)", "$n"], 53 decimals: 2, 54 ",": ",", 55 ".": ".", 56 groupSize: [3], 57 symbol: "$" 58 } 59 }, 60 calendars: { 61 standard: { 62 days: { 63 names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], 64 namesAbbr: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], 65 namesShort: [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ] 66 }, 67 months: { 68 names: ["January", "February", "March", "APRil", "May", "June", "July", "August", "September", "October", "November", "December"], 69 namesAbbr: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 70 }, 71 AM: [ "AM", "am", "AM" ], 72 PM: [ "PM", "pm", "PM" ], 73 patterns: { 74 d: "M/d/yyyy", 75 D: "dddd, MMMM dd, yyyy", 76 F: "dddd, MMMM dd, yyyy h:mm:ss tt", 77 g: "M/d/yyyy h:mm tt", 78 G: "M/d/yyyy h:mm:ss tt", 79 m: "MMMM dd", 80 M: "MMMM dd", 81 s: "yyyy'-'MM'-'ddTHH':'mm':'ss", 82 t: "h:mm tt", 83 T: "h:mm:ss tt", 84 u: "yyyy'-'MM'-'dd HH':'mm':'ss'Z'", 85 y: "MMMM, yyyy", 86 Y: "MMMM, yyyy" 87 }, 88 "/": "/", 89 ":": ":", 90 firstDay: 0, 91 twoDigitYearMax: 2029 92 } 93 } 94 }; 95 96 97 function findCulture(culture) { 98 if (culture) { 99 if (culture.numberFormat) {100 return culture;101 }102 103 if (typeof culture === STRING) {104 var cultures = chopper.cultures;105 return cultures[culture] || cultures[culture.split("-")[0]] || null;106 }107 108 return null;109 }110 111 return null;112 }113 114 function getCulture(culture) {115 if (culture) {116 culture = findCulture(culture);117 }118 119 return culture || chopper.cultures.current;120 }121 122 function expandNumberFormat(numberFormat) {123 numberFormat.groupSizes = numberFormat.groupSize;124 numberFormat.percent.groupSizes = numberFormat.percent.groupSize;125 numberFormat.currency.groupSizes = numberFormat.currency.groupSize;126 }127 128 chopper.culture = function(cultureName) {129 var cultures = chopper.cultures, culture;130 131 if (cultureName !== undefined) {132 culture = findCulture(cultureName) || cultures[EN];133 culture.calendar = culture.calendars.standard;134 cultures.current = culture;135 136 if (globalize && !globalize.load) {137 expandNumberFormat(culture.numberFormat);138 }139 140 } else {141 return cultures.current;142 }143 };144 145 146 chopper.culture(EN);147 148 //number formatting149 function formatNumber(number, format, culture) {150 culture = getCulture(culture);151 152 var numberFormat = culture.numberFormat,153 groupSize = numberFormat.groupSize[0],154 groupSeparator = numberFormat[COMMA],155 decimal = numberFormat[POINT],156 precision = numberFormat.decimals,157 pattern = numberFormat.pattern[0],158 literals = [],159 symbol,160 isCurrency, isPercent,161 customPrecision,162 formatAndPrecision,163 negative = number < 0,164 integer,165 fraction,166 integerLength,167 fractionLength,168 replacement = EMPTY,169 value = EMPTY,170 idx,171 length,172 ch,173 hasGroup,174 hasNegativeFormat,175 decimalIndex,176 sharpIndex,177 zeroIndex,178 hasZero, hasSharp,179 percentIndex,180 currencyIndex,181 startZeroIndex,182 start = -1,183 end;184 185 //return empty string if no number186 if (number === undefined) {187 return EMPTY;188 }189 190 if (!isFinite(number)) {191 return number;192 }193 194 //if no format then return number.toString() or number.toLocaleString() if culture.name is not defined195 if (!format) {196
新闻热点
疑难解答