Home Backend Development PHP Tutorial PHP数目字格式化,数字每三位加逗号,可以保留小数

PHP数目字格式化,数字每三位加逗号,可以保留小数

Jun 13, 2016 pm 01:19 PM
format rvalue substr

PHP数字格式化,数字每三位加逗号,可以保留小数

在报价的时候为了给浏览者更清晰明确的数字,所以需要用到数字格式化,有两种方法,一种自己写函数,另一种当然是系统自带的,其实我更喜欢系统自带的。

?

先来系统简单的:

string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] ):

?

查看代码1 echo number_format('169856420');

?

输出结果将为:169,856,420

?

查看代码1 echo number_format('1000000',2);

?

输出结果将为:1,000,000.00

?

查看代码1 echo number_format('1000000',2,',','.');

?

输出结果将为:1.000.000,00

?

再看写的函数:

?

function num_format($num){  

   if(!is_numeric($num)){  

       return false;  

     }  

     $num = explode('.',$num);//把整数和小数分开  

   $rl = $num[1];//小数部分的值  

   $j = strlen($num[0]) % 3;//整数有多少位  

    $sl = substr($num[0], 0, $j);//前面不满三位的数取出来  

    $sr = substr($num[0], $j);//后面的满三位的数取出来  

    $i = 0;  

     while($i 
<p>??</p>
 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

PHP formats a GMT/UTC date/time PHP formats a GMT/UTC date/time Mar 21, 2024 am 10:41 AM

This article will explain in detail how to format a GMT/UTC date/time with PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to Formatting GMT/UTC Date/Time in PHP In PHP, formatting GMT/UTC date/time is crucial for correctly displaying and handling cross-time zone dates. This article will explain how to format a GMT/UTC date/time using PHP's DateTime class, as well as the various formatting options available. DateTime class The DateTime class represents a date and time. It can store and manipulate date/time values ​​in time zones such as GMT/UTC. To create a new Da

What does format in python mean? What does format in python mean? Jul 31, 2023 pm 02:02 PM

format in Python is a string formatting method used to insert variables or values ​​into placeholder positions in a string. Through the format method, we can dynamically construct a string to contain different values.

What does format mean in python What does format mean in python Jul 31, 2023 pm 02:05 PM

In Python, `format` is a built-in function used to format strings. It is used to create a string template with placeholders and fill the placeholders with specified values. This allows strings to be constructed dynamically based on different situations, making the output more readable and customizable.

What is the usage of format in python What is the usage of format in python Jul 31, 2023 pm 01:59 PM

The usage of format in python is basic usage, specifying location, specifying variable name, formatting numbers, formatting date and time.

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

How to convert a string into formatted date and time in MySQL How to convert a string into formatted date and time in MySQL Jun 01, 2023 pm 09:22 PM

STR_TO_DATE(date, format): Convert the string into the date and time in format SELECTSTR_TO_DATE(‘2015-01-01’,‘%Y-%m-%d’)->2015-01-01

See all articles