รูปแบบคือการแปลงข้อมูลทุกประเภทเป็นสตริงผ่านการดำเนินการรูปแบบ ตัวอย่างเช่นสิ่งต่อไปนี้
การคัดลอกรหัสมีดังนี้:
<script>
console.log (chopper.format ('{0} - {1} - {2}', 12, 24, 25)); // เอาต์พุต "12 - 24 - 25"
</script>
นี่คือรหัสที่สมบูรณ์ที่สามารถคัดลอกไปยังโครงการของคุณเอง
การคัดลอกรหัสมีดังนี้:
<! doctype html>
<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8">
</head>
<body>
<script src = "http://code.jquery.com/jquery-1.9.1.min.js"> </script>
<script>
(การทำงาน() {
var chopper = window.chopper = window.chopper || {วัฒนธรรม: {}},
คณิตศาสตร์ = คณิตศาสตร์
formatregexp = // {(/d+) (: [^/}]+)?/}/g,
function = "function",
String = "String"
number = "number",
Object = "Object"
null = "null",
บูลีน = "บูลีน"
undefined = "ไม่ได้กำหนด"
slice = [] .slice,
โลกาภิวัตน์ = window.globalize
StandardFormatregexp =/^(n | c | p | e) (/d*) $/i,
literalregexp = /(//.)|( อยู่เหมือนกัน แต่เหมือนกัน
commaregexp = //,/g,
ว่าง = ""
จุด = ".",
เครื่องหมายจุลภาค = ",",
Sharp = "#"
zero = "0"
ตัวยึด = "??"
en = "en-us"
ObjectToString = {} .toString;
// วัฒนธรรม
Chopper.cultures ["en-us"] = {
ชื่อ: en,
numberFormat: {
รูปแบบ: ["-n"],
ทศนิยม: 2,
-
-
กลุ่ม: [3],
เปอร์เซ็นต์: {
รูปแบบ: ["-n %", "n %"],
ทศนิยม: 2,
-
-
กลุ่ม: [3],
เครื่องหมาย: "%"
-
สกุลเงิน: {
รูปแบบ: ["($ n)", "$ n"],
ทศนิยม: 2,
-
-
กลุ่ม: [3],
สัญลักษณ์: "$"
-
-
ปฏิทิน: {
มาตรฐาน: {
วัน: {
ชื่อ: ["วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"],
Namesabbr: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
Namesshort: ["su", "mo", "tu", "เรา", "th", "fr", "sa"]
-
เดือน: {
ชื่อ: ["มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม", "
Namesabbr: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
-
AM: ["Am", "Am", "AM"]
PM: ["PM", "PM", "PM"]
รูปแบบ: {
D: "M/D/YYYY"
D: "dddd, mmmm dd, yyyy",
F: "DDDD, MMMM DD, YYYY H: MM: SS TT"
G: "M/D/YYYY H: MM TT"
G: "M/D/YYYY H: MM: SS TT"
M: "MMMM DD"
M: "MMMM DD"
S: "yyyy '-' mm '-' ddthh ':' mm ':' ss",
t: "h: mm tt",
T: "H: MM: SS TT"
คุณ: "yyyy '-' mm '-' dd hh ':' mm ':' ss'z '",
Y: "MMMM, YYYY",
Y: "MMMM, YYYY"
-
-
-
วันแรก: 0,
TwodigityeArmax: 2029
-
-
-
ฟังก์ชั่น findCulture (วัฒนธรรม) {
ถ้า (วัฒนธรรม) {
if (culture.numberFormat) {
วัฒนธรรมการกลับมา;
-
if (typeof culture === สตริง) {
วัฒนธรรม var = Chopper.cultures;
กลับมาวัฒนธรรม [วัฒนธรรม] || วัฒนธรรม [culture.split ("-") [0]] || โมฆะ;
-
คืนค่า null;
-
คืนค่า null;
-
ฟังก์ชั่น getCulture (วัฒนธรรม) {
ถ้า (วัฒนธรรม) {
วัฒนธรรม = findCulture (วัฒนธรรม);
-
วัฒนธรรมกลับ || Chopper.cultures.current;
-
ฟังก์ชั่น expandNumberFormat (numberFormat) {
numberFormat.groupsizes = numberFormat.groupSize;
numberFormat.percent.groupsizes = numberFormat.percent.groupsize;
numberFormat.currency.groupsizes = numberFormat.currency.groupsize;
-
Chopper.culture = function (culturename) {
วัฒนธรรม var = Chopper.cultures วัฒนธรรม;
if (culturename! == undefined) {
Culture = FindCulture (Culturename) || วัฒนธรรม [en];
Culture.calendar = Culture.calendars.standard;
วัฒนธรรมปัจจุบัน = วัฒนธรรม;
if (โลกาภิวัตน์ &&! globalize.load) {
ExpandNumberFormat (culture.numberFormat);
-
} อื่น {
วัฒนธรรมกลับมาปัจจุบัน;
-
-
Chopper.Culture (EN);
// การจัดรูปแบบหมายเลข
Function formatNumber (จำนวน, รูปแบบ, วัฒนธรรม) {
วัฒนธรรม = getCulture (วัฒนธรรม);
var numberFormat = culture.numberFormat
กลุ่ม = numberformat.groupsize [0]
GroupsPearator = numberFormat [comma]
ทศนิยม = numberFormat [จุด]
ความแม่นยำ = numberFormat.decimals
รูปแบบ = numberFormat.pattern [0]
ตัวอักษร = []
เครื่องหมาย,
Iscurrency, ispercent,
CustomPrecision
Formatand และ Precision
ลบ = จำนวน <0,
จำนวนเต็ม,
เศษส่วน
Integerlength
FractionLength
เปลี่ยน = ว่างเปล่า
ค่า = ว่างเปล่า
idx,
ความยาว,
ch,
Hasgroup
HasnegativeFormat
Decimalindex
Sharpindex
ZeroIndex
Haszero, Hassharp,
เปอร์เซ็นต์
CurrencyIndex
StartzeroIndex
เริ่ม = -1,
จบ;
// ส่งคืนสตริงว่างถ้าไม่มีหมายเลข
if (number === ไม่ได้กำหนด) {
กลับว่างเปล่า;
-
if (! isfinite (หมายเลข)) {
หมายเลขคืน;
-
// ถ้าไม่มีรูปแบบให้ส่งคืนหมายเลข. toString () หรือ number.tolocalestring () หากไม่ได้กำหนด culture.name
if (! รูปแบบ) {
กลับ culture.name.length? number.tolocalestring (): number.toString ();
-
FormatandPrecision = StandardFormatRexp.exec (รูปแบบ);
// การจัดรูปแบบมาตรฐาน
if (formatandandprecision) {
format = formatandprecision [1] .toLowerCase ();
isCurrency = format === "C";
ispercent = format === "p";
if (iScurrency || ispercent) {
// รับข้อมูลรูปแบบตัวเลขเฉพาะหากรูปแบบเป็นสกุลเงินหรือเปอร์เซ็นต์
numberFormat = iScurrency? NumberFormat.currency: NumberFormat.percent;
กลุ่ม = numberFormat.groupsize [0];
GroupsPearator = numberFormat [comma];
ทศนิยม = numberFormat [จุด];
ความแม่นยำ = numberFormat.decimals;
Symbol = numberFormat.symbol;
รูปแบบ = numberFormat.pattern [ลบ? 0: 1];
-
customPrecision = formatandprecision [2];
if (customprecision) {
Precision = +CustomPrecision;
-
// หมายเลขส่งคืนในรูปแบบเลขชี้กำลัง
if (format === "e") {
ส่งคืน CustomPrecision? number.toexponential (ความแม่นยำ): number.toexponential (); // toexponential () และ toexponential (ไม่ได้กำหนด) แตกต่างกันใน FF #653438
-
// คูณถ้ารูปแบบเป็นเปอร์เซ็นต์
ถ้า (ispercent) {
หมายเลข *= 100;
-
number = round (จำนวน, ความแม่นยำ);
ลบ = จำนวน <0;
number = number.split (จุด);
จำนวนเต็ม = หมายเลข [0];
ส่วน = หมายเลข [1];
// ยกเว้น "-" ถ้าตัวเลขเป็นลบ
ถ้า (ลบ) {
Integer = Integer.substring (1);
-
ค่า = จำนวนเต็ม;
IntegerLength = Integer.length;
// เพิ่มตัวคั่นกลุ่มลงในหมายเลขหากยาวพอ
if (IntegerLength> = กลุ่ม) {
ค่า = ว่าง;
สำหรับ (idx = 0; idx <integerLength; idx ++) {
if (idx> 0 && (IntegerLength - idx) % กลุ่ม === 0) {
value += GroupsPearator;
-
value += integer.charat (idx);
-
-
ถ้า (เศษส่วน) {
ค่า + = ทศนิยม + ส่วน;
-
if (format === "n" &&! ลบ) {
ค่าส่งคืน;
-
จำนวน = ว่าง;
สำหรับ (idx = 0, length = pattern.length; idx <length; idx ++) {
CH = pattern.charat (idx);
ถ้า (ch === "n") {
หมายเลข += ค่า;
} อื่นถ้า (ch === "$" || ch === "%") {
หมายเลข += สัญลักษณ์;
} อื่น {
หมายเลข += ch;
-
-
หมายเลขคืน;
-
// การจัดรูปแบบที่กำหนดเอง
-
// รูปแบบแยกต่างหากตามส่วน
// สร้างหมายเลขเป็นบวก
ถ้า (ลบ) {
number = -number;
-
if (format.indexof ("")> -1 || format.indexof ("/" ")> -1 || format.indexof (" // ")> -1) {
format = format.replace (Literalregexp, ฟังก์ชั่น (จับคู่) {
var quotechar = match.charat (0). replace ("//", ""),
ตัวอักษร = match.slice (1). แทนที่ (quotechar, "");
ตัวอักษรพัช (ตัวอักษร);
ผู้ถือครองตำแหน่ง;
-
-
format = format.split (";");
ถ้า (ลบ && ฟอร์แมต [1]) {
// รับรูปแบบลบ
รูปแบบ = รูปแบบ [1];
hasnegativeformat = true;
} อื่นถ้า (number === 0) {
// รูปแบบสำหรับศูนย์
format = format [2] || รูปแบบ [0];
if (format.indexof (sharp) == -1 && format.indexof (zero) == -1) {
// ส่งคืนรูปแบบถ้าเป็นค่าคงที่สตริง
รูปแบบการส่งคืน;
-
} อื่น {
รูปแบบ = รูปแบบ [0];
-
PercentIndex = format.indexof ("%");
currencyIndex = format.indexof ("$");
ISPERCENT = PERCENTINDEX! = -1;
ISCurrency = CurrencyIndex! = -1;
// คูณจำนวนหากรูปแบบมีเปอร์เซ็นต์
ถ้า (ispercent) {
หมายเลข *= 100;
-
if (isCurrency && format [currencyIndex - 1] === "//") {
format = format.split ("//"). เข้าร่วม ("");
ISCurrency = FALSE;
-
if (iScurrency || ispercent) {
// รับข้อมูลรูปแบบตัวเลขเฉพาะหากรูปแบบเป็นสกุลเงินหรือเปอร์เซ็นต์
numberFormat = iScurrency? NumberFormat.currency: NumberFormat.percent;
กลุ่ม = numberFormat.groupsize [0];
GroupsPearator = numberFormat [comma];
ทศนิยม = numberFormat [จุด];
ความแม่นยำ = numberFormat.decimals;
Symbol = numberFormat.symbol;
-
hasgroup = format.indexof (comma)> -1;
ถ้า (hasgroup) {
format = format.replace (commaregexp, ว่าง);
-
decimalIndex = format.indexof (จุด);
ความยาว = format.length;
if (decimalIndex! = -1) {
fraction = number.toString (). แยก ("E");
ถ้า (เศษส่วน [1]) {
Fraction = Round (number, math.abs (เศษส่วน [1]));
} อื่น {
ส่วน = เศษส่วน [0];
-
fraction = fraction.split (จุด) [1] || ว่างเปล่า;
ZeroIndex = format.lastIndexof (Zero) - DecimalIndex;
sharpIndex = format.lastindexof (คมชัด) - DecimalIndex;
haszero = zeroIndex> -1;
HASSHARP = SharpIndex> -1;
idx = fraction.length;
if (! Haszero &&! HASSHARP) {
format = format.substring (0, DecimalIndex) + format.substring (DecimalIndex + 1);
ความยาว = format.length;
DecimalIndex = -1;
idx = 0;
} if (haszero && zeroIndex> sharpIndex) {
idx = ZeroIndex;
} อื่นถ้า (SharpIndex> ZeroIndex) {
if (hassharp && idx> sharpindex) {
idx = sharpindex;
} อื่นถ้า (haszero && idx <ZeroIndex) {
idx = ZeroIndex;
-
-
if (idx> -1) {
number = round (number, idx);
-
} อื่น {
number = round (number);
-
sharpIndex = format.indexof (คมชัด);
startzeroIndex = zeroIndex = format.indexof (zero);
// กำหนดดัชนีของตัวยึดหลักแรก
if (sharpindex == -1 && zeroIndex! = -1) {
start = ZeroIndex;
} อื่นถ้า (sharpindex! = -1 && zeroIndex == -1) {
start = sharpindex;
} อื่น {
Start = SharpIndex> ZeroIndex? ZeroIndex: SharpIndex;
-
sharpIndex = format.lastindexof (คมชัด);
zeroIndex = format.lastindexof (Zero);
// กำหนดดัชนีของตัวยึดหลักสุดท้าย
if (sharpindex == -1 && zeroIndex! = -1) {
สิ้นสุด = ZeroIndex;
} อื่นถ้า (sharpindex! = -1 && zeroIndex == -1) {
end = sharpindex;
} อื่น {
end = sharpindex> zeroIndex? SharpIndex: ZeroIndex;
-
if (start == ความยาว) {
สิ้นสุด = เริ่ม;
-
ถ้า (เริ่ม! = -1) {
value = number.toString (). แยก (จุด);
จำนวนเต็ม = ค่า [0];
Fraction = value [1] || ว่างเปล่า;
IntegerLength = Integer.length;
fractionLength = fraction.length;
ถ้า (ลบ && (หมายเลข * -1)> = 0) {
ลบ = เท็จ;
-
// เพิ่มตัวคั่นกลุ่มลงในหมายเลขหากยาวพอ
ถ้า (hasgroup) {
if (IntegerLength === กลุ่ม && integerLength <DecimalIndex - startzeroIndex) {
จำนวนเต็ม = groupsparator + จำนวนเต็ม;
} อื่นถ้า (IntegerLength> กลุ่ม) {
ค่า = ว่าง;
สำหรับ (idx = 0; idx <integerLength; idx ++) {
if (idx> 0 && (IntegerLength - idx) % กลุ่ม === 0) {
value += GroupsPearator;
-
value += integer.charat (idx);
-
จำนวนเต็ม = ค่า;
-
-
number = format.substring (0, start);
ถ้า (ลบ &&! hasnegativeFormat) {
หมายเลข += "-";
-
สำหรับ (idx = start; idx <length; idx ++) {
CH = format.charat (idx);
if (decimalIndex == -1) {
if (end - idx <integerLength) {
หมายเลข += จำนวนเต็ม;
หยุดพัก;
-
} อื่น {
if (zeroIndex! = -1 && zeroIndex <idx) {
แทนที่ = ว่าง;
-
if ((decimalIndex - idx) <= integerLength && decimalIndex - idx> -1) {
หมายเลข += จำนวนเต็ม;
idx = decimalIndex;
-
if (decimalIndex === idx) {
number + = (เศษส่วน? ทศนิยม: ว่าง) + ส่วน;
idx + = end - DecimalIndex + 1;
ดำเนินการต่อ;
-
-
ถ้า (ch === ศูนย์) {
หมายเลข += ch;
เปลี่ยน = ch;
} อื่นถ้า (ch === คม) {
หมายเลข += การเปลี่ยน;
-
-
if (end> = start) {
number + = format.substring (end + 1);
-
// แทนที่ตัวยึดสัญลักษณ์
if (iScurrency || ispercent) {
ค่า = ว่าง;
สำหรับ (idx = 0, length = number.length; idx <length; idx ++) {
ch = number.charat (idx);
ค่า += (ch === "$" || ch === "%")? สัญลักษณ์: ch;
-
หมายเลข = ค่า;
-
ความยาว = ตัวอักษรความยาว;
ถ้า (ความยาว) {
สำหรับ (idx = 0; idx <ความยาว; idx ++) {
number = number.retlace (ตัวยึดตัวอักษร [idx]);
-
-
-
หมายเลขคืน;
-
var round = function (ค่าความแม่นยำ) {
ความแม่นยำ = ความแม่นยำ || 0;
value = value.toString (). แยก ('e');
value = math.round ( + (ค่า [0] + 'e' + (ค่า [1]? ( + ค่า [1] + ความแม่นยำ): ความแม่นยำ)));
value = value.toString (). แยก ('e');
value = + (value [0] + 'e' + (ค่า [1]? ( + value [1] - ความแม่นยำ): -procision));
return value.tofixed (ความแม่นยำ);
-
var toString = ฟังก์ชั่น (ค่า, fmt, วัฒนธรรม) {
ถ้า (fmt) {
if (typeof value === จำนวน) {
return formatNumber (ค่า, FMT, วัฒนธรรม);
-
-
ค่าส่งคืน! == ไม่ได้กำหนด? ค่า : "";
-
if (โลกาภิวัตน์ &&! globalize.load) {
toString = function (ค่า, รูปแบบ, วัฒนธรรม) {
if ($ .isplainobject (วัฒนธรรม)) {
วัฒนธรรม = culture.name;
-
Return LOLOMALIZE.Format (ค่า, รูปแบบ, วัฒนธรรม);
-
-
Chopper.format = function (fmt) {
ค่า var = อาร์กิวเมนต์;
ส่งคืน fmt.replace (formatregexp, function (จับคู่, ดัชนี, placeholderformat) {
ค่า var = ค่า [parseInt (ดัชนี, 10) + 1];
ส่งคืน TOSTRING (มูลค่า, ตำแหน่งผู้ถือครอง?
-
-
-
</script>
</body>
</html>
API:
การคัดลอกรหัสมีดังนี้:
Chopper.Format ('{0} กำลังเล่น {1}', 'Xiaoming', 'Basketball'); // เอาท์พุท "Xiaoming กำลังเล่นบาสเก็ตบอล"
// ราคา
Chopper.Format ('{0: C} - {1: C}', 10, 20); // เอาต์พุต "10.00−20.00"
// ดัชนี
Chopper.Format ('ดัชนี: {0: e}', 25); // outputs "ดัชนี: 2.5E+1"
// เปอร์เซ็นต์
Chopper.Format ('เปอร์เซ็นต์: {0: P}', 25); // เอาต์พุต "เปอร์เซ็นต์: 2,500.00 %"
// ทศนิยม
Chopper.Format ('ทศนิยม: {0: n}', 25); // เอาต์พุต "ทศนิยม: 25.00"
สรุป:
ข้อมูลรูปแบบมักใช้ในการพัฒนา ตัวอย่างเช่นเราจำเป็นต้องแจ้งข้อมูลที่แตกต่างกันตามตัวแปร แต่เทมเพลตเนื้อหาเหมือนกันดังนั้นเราจึงสามารถใช้วิธีนี้ได้ หากโครงการของคุณใช้ jQuery คุณสามารถห่อหุ้มจาวาสคริปต์ด้านบนลงในปลั๊กอิน jQuery