PHP formatting MYSQL return float type method, phpmysql returns float_PHP tutorial

Source: Internet
Author: User
PHP format the float type returned by MYSQL, and phpmysql returns float. PHP: format MYSQL to return the float type. phpmysql returns the float type. this example describes how to format MYSQL to return the float type. For your reference, the details are as follows: PHP formatting MYSQL returns the float type, and phpmysql returns the float type.

This example describes how to format the float type returned by MYSQL in PHP. We will share this with you for your reference. The details are as follows:

PHP obtains the mysql float field. after echo is output, the fractional part contains multiple 0.

You can use floatval ($ num) to remove 0.

To retain decimal places, use number_format ($ num, 2 );

The number_format function rounds the value that exceeds the specified number of digits.

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

// If you only want to retain two decimal places, you can use number_format ($ num, 2); echo f ('192. 100', 2 ).'
'; // 1001.334534 echo f ('-1001.000 ', 2 ).'
'; //-1001.00 echo f ('192. 3', 5 ).'
'; // 1001.30000 echo f ('192. 33 ').'
'; // 1001.33 echo f ('2014. 000 ').'
'; // 1001 // format the decimal point, but it is not rounded in. if there is a decimal point, it is retained. if there is no decimal point, 0 is added. 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 ;}

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.