简体   繁体   中英

rounding a number, NOT necessarily decimel PHP

I have a question.

I am using php to generate a number based on operations that a user has specified

This variable is called

$new

$new is an integer, I want to be able to round $new to a 12 digit number, regardless of the answer

I was thinking I could use round() or ceil()

but I believe these are used for rounding decimel places


So, I have an integer stored in $new, when $new is echoed out I want for it to print 12 digits. Whether the number is 60 billion or 0.00000000006

If i understand correctly

function showNumber($input) {
  $show = 12;
  $input = number_format(min($input,str_repeat('9', $show)), ($show-1) - strlen(number_format($input,0,'.','')),'.','');

  return $input;
}

var_dump(showNumber(1));
var_dump(showNumber(0.00000000006));
var_dump(showNumber(100000000000000000000000));

gives

string(12) "1.0000000000"
string(12) "0.0000000001"
string(12) "999999999999"

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