PHP format MySQL return float type method _php tips

Source: Internet
Author: User
Tags php introduction

The example in this article describes the way PHP formats MySQL to return the float type. Share to everyone for your reference, specific as follows:

PHP to obtain the MySQL float field, echo output, the decimal part is divided into more than 0.

You can use Floatval ($num) to go to 0 homes.

If you want to keep the decimal places, you can use Number_format ($num, 2);

The Number_format function is rounded up for a value that exceeds the specified number of digits.

If you do not want to be rounded, keep all decimals. You can use the following methods:

If you want to keep only two decimal places available Number_format ($num, 2);
echo f (' 1001.334534 ', 2). ' <br> '; 1001.334534
Echo F ('-1001.000 ', 2). ' <br> '; -1001.00
Echo F (' 1001.3 ', 5). ' <br> '; 1001.30000
Echo F (' 1001.33 '). ' <br> '; 1001.33
Echo F (' 1001.000 '). ' <br> '; 1001
//Format decimal, but not rounded, if there is a decimal is all reserved, no decimal is added 0;
function f ($num, $v = 0)
{
  $num = floatval ($num);
  if ($v > 0)
  {
    $num = '. $num;
    $arr = Explode ('. ', $num);
    if (count ($arr) = = 1)
    {
      $num. = '. '. Str_repeat (' 0 ', $v);
    }
    else
    {
      $v-= strlen ($arr [1]);
      if ($v > 0)
        $num. = Str_repeat (' 0 ', $v);
    }
  return $num;
}

More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary

I hope this article will help you with the PHP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.