함수 toFixed()

1 Java[ | ]

System.out.print( String.format("%.2f", 5.1234) );   // 5.12
System.out.print( String.format("%.2f", 5.99999) );  // 6.00
System.out.print( String.format("%.2f", 5.0) );      // 5.00
System.out.print( String.format("%.2f", (float)5) ); // 5.00

System.out.print( String.format("%.2f", 1000.1234) ); // 1000.12

2 JavaScript[ | ]

console.log( (5.1234).toFixed(2) ); // 5.12
console.log( (5.9999).toFixed(2) ); // 6.00
console.log( (5).toFixed(2) ); // 5.00

console.log( (1000.1234).toFixed(2) ); // 1000.12
var num = 2/3;
console.log( 100*num.toFixed(1) ); // 70
console.log( (100*num).toFixed(1) ); // 66.7

3 PHP[ | ]

echo number_format(5.1234, 2); # 5.12
echo number_format(5.9999, 2); # 6.00
echo number_format(5, 2); # 5.00

echo number_format(1000.1234, 2);          # 1,000.12
echo number_format(1000.1234, 2, '.', ''); # 1000.12

4 Python[ | ]

print( '{:0.0f}'.format(12.34) ) # 12
print( '{:0.2f}'.format(12.34) ) # 12.34
print( '{:0.4f}'.format(12.34) ) # 12.3400

print( '%0.0f' % 12.34 ) # 12
print( '%0.2f' % 12.34 ) # 12.34
print( '%0.4f' % 12.34 ) # 12.3400

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}