Number.prototype.toFixed()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
toFixed() ë©ìëë ì«ì를 ê³ ì ììì í기ë²(fixed-point notation)ì¼ë¡ íìí©ëë¤.
ìëí´ ë³´ê¸°
function financial(x) {
return Number.parseFloat(x).toFixed(2);
}
console.log(financial(123.456));
// Expected output: "123.46"
console.log(financial(0.004));
// Expected output: "0.00"
console.log(financial("1.23e+5"));
// Expected output: "123000.00"
구문
numObj.toFixed([digits]);
매ê°ë³ì
digitsOptional-
ììì ë¤ì ëíë ì릿ì. 0 ì´ì 20 ì´íì ê°ì ì¬ì©í ì ìì¼ë©°, 구íì²´ì ë°ë¼ ë ëì ë²ìì ê°ì ì§ìí ìë ììµëë¤. ê°ì ì§ì íì§ ìì¼ë©´ 0ì ì¬ì©í©ëë¤.
ë°í ê°
ê³ ì ììì í기ë²ì ì¬ì©íì¬ ëíë¸ ì를 문ìì´ë¡ ë°ê¾¼ ê°.
ìì¸
RangeError-
digitsê° ë무 ìê±°ë ë무 í´ ë. ê°ì´ 0ê³¼ 100ì¬ì´ì ê°ì´ë¼ë©´RangeError를 ì ë°íì§ ììµëë¤. 구íì²´ì ë°ë¼ ë í¬ê±°ë ìì ê°ì ì§ìí ì ììµëë¤. TypeError-
Numberê° ìë ê°ì²´ìì í¸ì¶í ê²½ì°.
ì¤ëª
toFixed()ë Number ê°ì²´ë¥¼ 주ì´ì§ digits ë§í¼ì ììì ì´í ì리ì를 ì ííê² ê°ë 문ìì´ ííì¼ë¡ ë°íí©ëë¤. ììì ì´íê° ê¸¸ë©´ ì«ì를 ë°ì¬ë¦¼íê³ , ì§§ìì ë¶ì¡±í ê²½ì° ë¤ë¥¼ 0ì¼ë¡ ì±ì¸ ì ììµëë¤. ë©ìë를 í¸ì¶í ì«ìì í¬ê¸°ê° 1e+21ë³´ë¤ í¬ë¤ë©´ Number.prototype.toString()ì í¸ì¶íì¬ ë°ì ì§ì íê¸°ë² ê²°ê³¼ë¥¼ ëì ë°íí©ëë¤.
ìì
>toFixed() ì¬ì©í기
var numObj = 12345.6789;
numObj.toFixed(); // Returns '12346': ë°ì¬ë¦¼íë©°, ìì ë¶ë¶ì ë¨ê¸°ì§ ììµëë¤.
numObj.toFixed(1); // Returns '12345.7': ë°ì¬ë¦¼í©ëë¤.
numObj.toFixed(6); // Returns '12345.678900': ë¹ ê³µê°ì 0ì¼ë¡ ì±ìëë¤.
(1.23e20).toFixed(2); // Returns '123000000000000000000.00'
(1.23e-10).toFixed(2); // Returns '0.00'
(2.34).toFixed(1); // Returns '2.3'
(2.35).toFixed(1); // Returns '2.4'. ì´ ê²½ì°ìë ì¬ë¦¼ì í©ëë¤.
-(2.34).toFixed(1); // Returns -2.3 (ì°ì°ìì ì ì©ì´ ì°ì ì´ê¸° ë문ì, ììì ê²½ì° ë¬¸ìì´ë¡ ë°ííì§ ììµëë¤...)
(-2.34).toFixed(1); // Returns '-2.3' (...ê´í¸ë¥¼ ì¬ì©í ê²½ì° ë¬¸ìì´ì ë°íí©ëë¤.)
ëª ì¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-number.prototype.tofixed> |