ฉันยังใหม่กับ JavaScript และเพิ่งค้นพบtoFixed()และtoPrecision()ปัดเศษตัวเลข อย่างไรก็ตามฉันคิดไม่ออกว่าความแตกต่างระหว่างทั้งสองคืออะไร
อะไรคือความแตกต่างระหว่างnumber.toFixed()และnumber.toPrecision()?
ฉันยังใหม่กับ JavaScript และเพิ่งค้นพบtoFixed()และtoPrecision()ปัดเศษตัวเลข อย่างไรก็ตามฉันคิดไม่ออกว่าความแตกต่างระหว่างทั้งสองคืออะไร
อะไรคือความแตกต่างระหว่างnumber.toFixed()และnumber.toPrecision()?
คำตอบ:
toFixed(n)ให้nความยาวหลังจุดทศนิยม toPrecision(x)ให้xความยาวรวม
อ้างอิงที่ w3schools: toFixedและtoPrecision
แก้ไข :
ฉันได้เรียนรู้มาระยะหนึ่งแล้วว่า w3schools ไม่ใช่แหล่งที่ดีที่สุด แต่ฉันลืมเกี่ยวกับคำตอบนี้จนกระทั่งฉันเห็นความคิดเห็นที่ "กระตือรือร้น" ของ kzh นี่ refs เพิ่มเติมจาก Mozilla ศูนย์หมอมีสำหรับtoFixed()และสำหรับtoPrecision()โชคดีสำหรับพวกเราทุกคน MDC และ w3schools เห็นพ้องกันในกรณีนี้
เพื่อความสมบูรณ์ฉันควรพูดถึงสิ่งtoFixed()นั้นเทียบเท่ากับtoFixed(0)และtoPrecision()เพียงแค่ส่งคืนตัวเลขเดิมโดยไม่มีการจัดรูปแบบ
toPrecision(x)ไม่ได้ "ระบุxความยาวรวม" แต่จะจัดรูปแบบเป็นตัวเลขสำคัญที่กำหนด ยกตัวอย่างเช่นจะกลับมา0.0000022.toPrecision(1) 0.000002
toPrecision(x)ระบุxความยาวทั้งหมด" ไม่จำเป็นต้องถือ ตัวอย่างตัวนับ:0.00001234.toPrecision(3)
ฉันเชื่อว่าในอดีตให้จำนวนตำแหน่งทศนิยมที่แน่นอนในขณะที่ค่าหลังให้เลขนัยสำคัญคงที่
Math.PI.toFixed(2); // "3.14"
Math.PI.toPrecision(2); // "3.1"
นอกจากนี้toPrecisionจะให้สัญกรณ์ทางวิทยาศาสตร์หากมีตัวเลขจำนวนเต็มมากกว่าความแม่นยำที่ระบุ
(Math.PI * 10).toPrecision(2); // "31"
(Math.PI * 100).toPrecision(2); // "3.1e+2"
แก้ไข: อ้อและถ้าคุณยังใหม่กับ JavaScript ฉันขอแนะนำหนังสือ " JavaScript: The Good Parts " ของ Douglas Crockford
ตัวอย่างพูดชัดเจน:
var A = 123.456789;
A.toFixed() // 123
A.toFixed(0) // 123
A.toFixed(1) // 123.5
A.toFixed(2) // 123.46
A.toFixed(3) // 123.457
A.toFixed(4) // 123.4568
A.toFixed(5) // 123.45679
A.toFixed(6) // 123.456789
A.toFixed(7) // 123.4567890
A.toFixed(8) // 123.45678900
A.toFixed(9) // 123.456789000
A.toFixed(10) // 123.4567890000
A.toFixed(11) // 123.45678900000
A.toPrecision() // 123.456789
A.toPrecision(0) // --- ERROR ---
A.toPrecision(1) // 1e+2
A.toPrecision(2) // 1.2e+2
A.toPrecision(3) // 123
A.toPrecision(4) // 123.5
A.toPrecision(5) // 123.46
A.toPrecision(6) // 123.457
A.toPrecision(7) // 123.4568
A.toPrecision(8) // 123.45679
A.toPrecision(9) // 123.456789
A.toPrecision(10) // 123.4567890
A.toPrecision(11) // 123.45678900
ฉันคิดว่านี่เป็นคำตอบที่ดีที่สุดด้วยตัวอย่าง
สมมติว่าคุณมีข้อมูลต่อไปนี้:
var products = [
{
"title": "Really Nice Pen",
"price": 150
},
{
"title": "Golf Shirt",
"price": 49.99
},
{
"title": "My Car",
"price": 1234.56
}
]
คุณต้องการแสดงผลิตภัณฑ์เหล่านี้พร้อมชื่อและราคาที่จัดรูปแบบ มาลองใช้toPrecisionก่อน:
document.write("The price of " + products[0].title + " is $" + products[0].price.toPrecision(5));
The price of Really Nice Pen is $150.00
ดูดีดังนั้นคุณอาจคิดว่าสิ่งนี้จะใช้ได้กับผลิตภัณฑ์อื่น ๆ เช่นกัน:
document.write("The price of " + products[1].title + " is $" + products[2].price.toPrecision(5));
document.write("The price of " + products[2].title + " is $" + products[2].price.toPrecision(5));
The price of Golf Shirt is $49.990
The price of My Car is $1234.6
ไม่ค่อยดี. เราสามารถแก้ไขปัญหานี้ได้โดยการเปลี่ยนจำนวนเลขนัยสำคัญสำหรับแต่ละผลิตภัณฑ์ แต่ถ้าเรากำลังทำซ้ำในชุดผลิตภัณฑ์ที่อาจยุ่งยาก มาใช้toFixedแทน:
document.write("The price of " + products[0].title + " is $" + products[0].price.toFixed(2));
document.write("The price of " + products[1].title + " is $" + products[2].price.toFixed(2));
document.write("The price of " + products[2].title + " is $" + products[2].price.toFixed(2));
The price of Really Nice Pen is $150.00
The price of Golf Shirt is $49.99
The price of My Car is $1234.56
สิ่งนี้ก่อให้เกิดสิ่งที่คุณคาดหวัง ไม่มีงานเดาที่เกี่ยวข้องและไม่มีการปัดเศษ
แค่:
49.99.toFixed(5)
// → "49.99000"
49.99.toPrecision(5)
// → "49.990"
ภายใต้สถานการณ์บางอย่างtoPrecision()จะส่งกลับสัญกรณ์เอกซ์โพเนนเชียลในขณะที่toFixed()จะไม่
a = 999999999999999934464;, ผลตอบแทนa.toFixed(0) "1e+21"บางทีคำตอบที่ถูกต้องกว่าอาจเป็นไปได้ว่า toFixed () ไม่ส่งคืนสัญกรณ์เอกซ์โพเนนเชียลเว้นแต่ toString () จะทำ
ตัวอย่างเช่นเราพิจารณาตัวแปร a เป็น var a = 123.45 a.toPrecision (6) ผลลัพธ์คือ 123.450 a.toFixed (6) ผลลัพธ์เป็นเช่น 123.450000 // 6 หลักหลังจุดทศนิยม
ทั้งสองtoPrecision()และtoFixed()เป็นฟังก์ชันที่ออกแบบมาเพื่อจัดรูปแบบตัวเลขก่อนที่จะพิมพ์ออกมา ดังนั้นทั้งสองจึงคืนStringค่า
มีข้อยกเว้นอย่างหนึ่ง หากคุณใช้ฟังก์ชันเหล่านี้บนลิเทอรัล Number เชิงลบเนื่องจากลำดับความสำคัญของตัวดำเนินการจะส่งคืน Number สิ่งนี้หมายความว่าtoFixed()หรือtoPrecision()จะส่งคืนสตริงก่อนจากนั้นตัว-ดำเนินการลบจะแปลงสตริงกลับเป็นตัวเลขเป็นค่าลบ โปรดดูตัวอย่างด้านล่าง
toPrecision()ส่งกลับค่าที่เป็นStringตัวแทนของอ็อบเจ็กต์ Number ในเครื่องหมายจุดคงที่หรือเลขชี้กำลังที่ปัดเศษเป็นเลขนัยสำคัญ ดังนั้นหากคุณระบุว่าคุณต้องการความแม่นยำเป็น 1 มันจะส่งคืนเลขนัยสำคัญแรกพร้อมกับสัญกรณ์ทางวิทยาศาสตร์เพื่อระบุเลขยกกำลังของ 10 หรือ 0 ก่อนหน้านี้ก่อนจุดทศนิยมหากเลขนัยสำคัญคือ <0
const num1 = 123.4567;
// if no arguments are passed, it is similar to converting the Number to String
num1.toPrecision(); // returns "123.4567
// scientific notation is used when you pass precision count less than total
// number of digits left of the period
num1.toPrecision(2); // returns "1.2e+2"
// last digit is rounded if precision is less than total significant digits
num1.toPrecision(4); // returns "123.5"
num1.toPrecision(5); // returns "123.46"
const largeNum = 456.789;
largeNum.toPrecision(2); // returns "4.6e+2"
// trailing zeroes are added if precision is > total digits of the number or float
num1.toPrecision(9); // returns "123.456700"
const num2 = 123;
num2.toPrecision(4); // returns "123.0"
const num3 = 0.00123;
num3.toPrecision(4); // returns "0.001230"
num3.toPrecision(5); // returns "0.0012300"
// if the number is < 1, precision is by the significant digits
num3.toPrecision(1); // returns "0.001"
toFixed()ส่งกลับการStringแสดงวัตถุ Number ในสัญกรณ์จุดคงที่ปัดขึ้น ฟังก์ชันนี้สนใจเฉพาะตัวเลขจุดทศนิยม
const num1 = 123.4567;
// if no argument is passed, the fractions are removed
num1.toFixed(); // returns "123"
// specifying an argument means you the amount of numbers after the decimal point
num1.toFixed(1); // returns "123.5"
num1.toFixed(3); // returns "123.457"
num1.toFixed(5); // returns "123.45670"
num1.toFixed(7); // returns "123.4567000"
// trying to operator on number literals
2.34.toFixed(1); // returns "2.3"
2.toFixed(1); // returns SyntaxError
(2).toFixed(1); // returns "2.0"
(2.34e+5).toFixed(1); // returns "234000.0"
ฉันได้กล่าวไว้ข้างต้นข้อยกเว้นที่การใช้ฟังก์ชันเหล่านี้บนตัวอักษรจำนวนลบจะส่งคืน Number และไม่ใช่ String เนื่องจากความสำคัญของตัวดำเนินการ นี่คือตัวอย่างบางส่วน:
// Note: these are returning as Number
// toPrecision()
-123.45.toPrecision(); // returns -123.45
-123.45.toPrecision(2); // returns -120
-123.45.toPrecision(4); // returns -123.5
-2.34e+2.toPrecision(1); // returns -200
-0.0456.toPrecision(1); // returns -0.05
-0.0456.toPrecision(6); // returns -0.0456
// toFixed()
-123.45.toFixed(); // returns -123.45
-123.45.toFixed(1); // returns -123.5
-123.45.toFixed(4); // returns -123.45
-0.0456.toFixed(1); // returns -0
-0.0456.toFixed(6); // -0.0456
ข้อเท็จจริงที่น่าสนใจ: มีเครื่องหมายศูนย์ตามที่เห็น -0.0456.toFixed(1)