PHP怎麼定義保留2位小數的變數

時間 2021-05-05 19:44:17

1樓:

<?php

$a=2;

$b=3;

$c=number_format($a*$b,2);

echo $c;

?>

主要是函式number_format 這個函式還有其他更多的功能,你去查一下手冊吧。

2樓:

在php中要保留兩位小數的方法有很多種辦法,有如:printf,substr,number_format,round等等方法

方法一sprintf()函式 ,sprintf() 函式把格式化的字串寫寫入一個變數中

<?php

$number = 123;

$txt = sprintf("%f",$number);

echo $txt;

?>

輸出:123.000000

方法二 substr()函式

$num = 123213.666666;

echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2));

方法三 number_format()函式

$number = 1234.5678;

$nombre_format_francais = number_format($number, 2, ',', ' '); // 1234,57

$english_format_number = number_format($number, 2, '.', '');   // 1234.57(我一般用這個)

方法四 round 函式,round() 函式對浮點數進行四捨五入。

<?php

echo(round(0.60));

echo(round(0.50));

echo(round(0.49));

echo(round(-4.40));

echo(round(-4.60));

?>

輸出:110

-4-5

如果要保留小數,後來引數根保留小數位數即可。

$number = 1234.5678;

echo round($number ,2); //1234.57

3樓:匿名使用者

<?php

$a = 3;

$b = 4;

$c = sprintf("%.2f",$a*$b);//sprintf類似於c中的printf,只是沒有輸出,返回格式化後的變數

echo $c;

?>

php如何保留2位小數 5

4樓:

要分2種情況。

1、數值不變,只在輸出時保留2位小數。

echo sprintf('%.2f', 3.1415);

2、數值上保留2位

echo round(3.1415, 2);

5樓:醉7清風

<?=number_format($buyt,2,'.',',')?>

php 如何保留2位小數

6樓:一騎當後

今天處理數

據的時候因為要保留2位小數,查版看幫助手權冊

<?php

$number = 1234.56;

// english notation (default)

$english_format_number = number_format($number);

// 1,235

// french notation

$nombre_format_francais = number_format($number, 2, ',', ' ');

// 1 234,56

$number = 1234.5678;

// english notation without thousands seperator

$english_format_number = number_format($number, 2, '.', '');

// 1234.57

?>

7樓:夜半凍檸樂

要分2種情況。

1、數值不變,只在輸出時保留2位小數。

echo sprintf('%.2f', 3.1415);

2、數值上保留2位

echo round(3.1415, 2);

8樓:親愛的歐子

round($number ,2);

php 如何給整數後2位加一個小數點

9樓:匿名使用者

解決思路如下(具體過程在**中有註釋):

10樓:匿名使用者

<?php

$a = 12345;

$a = substr_replace($a, ',', -2, 0);

echo $a;

?>

想換成別的把第二個引數改了就行

11樓:匿名使用者

<?php

$a=你的數字;

$str=",";

if($a>99)else

}else

?>

想用什麼分開修改$str裡的值就版是了權

12樓:匿名使用者

最簡單的方法是:

<?php

$a=123;

$a=$a/100;

echo $a;

?>

用php使數字保留小數點後兩位怎麼做的?

13樓:匿名使用者

php 中的 round() 函式可以實現round() 函式對浮點數

進行四捨五入。

round(x,prec)

引數說明

x 可選專

。規屬定要舍入的數字。

prec 可選。規定小數點後的位數。

返回將 x 根據指定精度 prec (十進位制小數點後數字的數目)進行四捨五入的結果。prec 也可以是負數或零(預設值)。

註釋:php 預設不能正確處理類似 "12,300.2" 的字串。

例如:<?php

echo round(-4.635,2);

?>

輸出: -4.64

14樓:匿名使用者

給你舉個例子

$n=round(1.95583, 2);

這是四捨五入法保留2位小數

15樓:賈桀南凝絲

使用round

函式:$number = 1.23456;

echo round($number, 2); // 1.23

php 保留小數點後2位

16樓:匿名使用者

$number= '124345.67888';

$p= stripos($number, '.');

echo substr($number,0,$p+3);

這個沒有四捨五入

17樓:匿名使用者

$number是一copy個小數

保留小數點後兩位

round($number,2, php_round_half_up) //不四捨五入,如果小數點兩位後有值

則進一位

round($number,2, php_round_half_down) //不四捨五入,如果小數點兩位後有值則捨去

18樓:匿名使用者

<?php

$i = 2.23343;

echo round($i,2);

?>

就可以保留小數點後兩位了。

希望對你幫助。

19樓:程遠皓

number_format($number, 2, '.', '');

round($number,2, php_round_half_down) 第三個引數需要php版本在5.3以上!

20樓:常道者

兩種取值法,看你需要什麼結果:

1 四捨五入(簡單,自帶函式就可以解決)

$youwantnum = sprintf('%.2f', $num);

//有人說用回floor,floor是取整的函式,答無法直接取小數;但可以轉換得到結果,那就是下面的例子

2 捨去後面的,不管是什麼

/**$num 要處理的浮點數

*$digits 保留的小數位數

* 實現思路:先乘以10的小數位數次方,用floor向下取整,再除以除數得到捨去後面位數的結果

* 最後再用sprintf配合位數再取一次值(此處是為了解決有些數字,最後一位為零時不顯示問題)

*/function floorfloat($num, $digits)

php如何保留小數點後2位並且取整?

21樓:常道者

/**$num 要處理的浮點數

*$digits 保留的小數位數

* 實現思路:先乘以10的小數位數次方,用floor向下取整,再除以除數得到捨去後面位數的結果

* 最後再用sprintf配合位數再取一次值(此處是為了解決有些數字,最後一位為零時不顯示問題)

*/function floorfloat($num, $digits)

22樓:匿名使用者

又保留兩位小數又要取整?細說一下什麼意思?請舉個實際的數字列子

23樓:匿名使用者

round($float,$precision);返回將 $float 根據指定精度 $precision(十進位制小數點後數字的數目)進行四捨五入的結果。

php如何保留一位小數,包括0,內詳?

24樓:it互聯天下

php變數保留一位小數,包括0;可以考慮使用sprintf函式,控制浮點數格式。示例如下:

<?php

header("content-type:text/html;charset=utf-8;");

$money=20;

$money=sprintf("%.1f",$money);

echo $money;

//20.0

$money2=20.20;

$money2=sprintf("%.1f",$money2);

echo $money2;

//20.2

25樓:白痴狼

number_format(變數,保留位數)

26樓:初縱

$number=number_format($number, 1);

PHP如何保留2位小數,PHP如何保留2位小數

要分2種情況。1 數值不變,只在輸出時保留2位小數。echo sprintf 2f 3.1415 2 數值上保留2位 echo round 3.1415,2 醉7清風 php 如何保留2位小數 一騎當後 今天處理數 據的時候因為要保留2位小數,查版看幫助手權冊 number 1234.56 engl...

VB向上保留一位小數,VB如何保留2位小數

function roundup x as double,k as integer as double roundup int x 10 k 0.9 10 k end function private sub command1 click 用法舉例 msgbox roundup 123.1111,1...

access查詢如何保留2位小數

知道小爺 access查詢中保留2位小數一般採用四捨五入的方式,需要用到round函式。工具 access2013 步驟 1 設定使用的小數為 20.35678 2 使用語句使之四捨五入保留2位小數。select round 20.35678,2 3 查詢結果 4 這樣就能使結果按照四捨五入的方式保...