2011-11-08 1 views
3
//Value being parsed at this point is "150.00" 
var productValue = parseFloat($("#product-cost-value-value").text().replace(',', '.'));  

console.log(productValue); 

기록되는 값은 150입니다.자바 스크립트 값에 0.00 이외의 값이없는 경우 소수점이 표시되지 않습니다.

그러나 여기에 약간의 값을 추가하면됩니다. 값이 변경되어 원하는 두 자릿수가 표시됩니다. 기록되는

console.log(productValue + 0.01); 

이 값

150.01

입니다

어떻게 내 값을 모두 한 번에 두 개의 소수 자릿수를 표시하기를 강제 할 수 있습니까?

+0

중복 가능성 [자바 스크립트의 float를 포맷하는 방법?] (http://stackoverflow.com/questions/661562/how -to-format-a-float-in-javascript) –

+0

가능한 복제본 [소수점 이하 2 자리] (http://stackoverflow.com/questions/6525335/trim-to-2-decimals) – Phrogz

답변

8

사용 :

console.log(productValue.toFixed(2)); 
3

사용 productValue.toFixed(2) 2 소수점

을 보여
관련 문제