简体   繁体   中英

remove remainder from string in php

Simple question. I have this code:

total = 41
win   = 48

       echo ($total/$win) * 100 ; 

printing out

85.416666666667

I need to remove the remainder so it prints out: 85 %.

http://www.ideone.com/7JFkI

echo floor(($total/$win) * 100) . '%';

Depending on how to want to round the number you may need to replace floor() with one of

  • floor() (9.4 → 9, 9.7 → 9)
  • round() (9.4 → 9, 9.7 → 10)
  • ceil() (9.4 → 10, 9.7 → 10)

Use the round(); function.

<?php
$total = 41;
$win   = 48;

echo round(($total/$win)*100).' %'; 
?>

the elegant way would be to use string

number_format(float $number, int $decimals, string $dec_point, string $thousands_sep);

like this:

<?php
$total = 41;
$win   = 48;

echo number_format(($total/$win)*100,0,'.').' %'; 
?>

您可以使用floor功能:

echo floor(($total/$win) * 100) ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM