2015-10-07 74 views
-2

我的道歉,如果這是以前討論過,但根據我從不同的討論,他們都沒有爲我工作的研究和觀察。我有一個計算一定比例的公式。我的問題是,它必須只有2位小數。它顯示像12.1212121212%而不是12.12%2位小數輸出

這是我的公式,並通過我使用php代碼的方式。

$p = $c/$t * 100 
+2

使用'.toFixed(2);'結果。 '$ P =($ C/$ T * 100).toFixed(2);'如果你想在Javascript – Tushar

+0

使用'number_format'解決 – Suyog

+0

它應該輪2位小數[R只是第2位小數? –

回答

2

您可以使用number_format()在PHP

$p = number_format((float)($c/$t * 100), 2, '.', ''); 

在JavaScript中,你可以使用toFixed(2)

var $c = 1, 
 
    $t = 1333, 
 
    $p = ($c/$t * 100).toFixed(2); 
 
document.write($p);

+0

文件撰寫($ p.toFixed(2));是嗎? – Vista