2015-10-07 116 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