Number formatting: PHP function number_format()

Table of contents:

Number formatting: PHP function number_format()
Number formatting: PHP function number_format()
Anonim

PHP function

number_format() is intended for bitwise formatting of numbers with separation of the fractional part. The specific format of the received value depends on the number of arguments passed. The function can take one, two or four parameters and returns the processed value as a string.

Formatted number

The first and required argument to the

number_format() function in PHP is the actual number to be formatted. The expression will look like this:


string number_format(float $number)

Parameter

$number must be a floating point number.

The result of the function will be a string containing the original number, divided by digits. The fractional part will be discarded with rounding. The comma is used as a separator of digits.

In the second example, the fractional part was rounded off.

Fractional part

If you want to preserve the fractional part, you need to pass the second argument to the PHP function

number_format().


string number_format(float $number, int $decimals)

$decimals is an integer,specifying the number of decimal places to store. By default, it is zero, so when passing one argument, the fractional part is discarded.

After accepting two parameters, the function will return a formatted string, using a dot as the decimal separator.

In this case, the fractional part is also rounded off. Zeros are substituted for missing digits.

How the PHP number_format function works
How the PHP number_format function works

Custom separators

Standard delimiters used by the PHP function

number_format() can be changed to any other characters. The third parameter is responsible for the separator of the fractional part, and the fourth - for the interdigit character.


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

Both

$dec_point

and

$thousands_sep are string data. You can pass any character as a delimiter, as well as a space or an empty string. From the sequence of characters, only the first one will be used, the rest will be cut off.

The php number_format function works in examples with four parameters:

The separators are replaced by custom ones, all other rules are preserved.

Custom delimiters in number_format PHP function
Custom delimiters in number_format PHP function

The PHP function

number_format() gives the programmer a lot of freedom in formatting numeric values. To obtain an adequate result, it is important to follow the syntax established by the specification and remember thatthat the return value of the function is of string type.

Popular topic

Editor's choice

  • Restore Skype account: step by step instructions, return access to your account
    Restore Skype account: step by step instructions, return access to your account

    Don't know how to recover your Skype account? Or do you want to know your friend's Skype password? The article discusses several ways to restore the user's page, as well as recommendations for storing and selecting pin codes

  • Why can't I follow on Instagram? All possible reasons
    Why can't I follow on Instagram? All possible reasons

    We subscribe to people with a specific goal - to find friends, like-minded people of interest, to learn something new for ourselves. And sometimes it is very strange to see that the Instagram system does not allow you to "follow" the profile you are interested in. This error occurs quite often, no one is immune from this trouble. Let's analyze the main reasons why Instagram does not allow you to subscribe to people

  • How to find out who "liked" a person on Instagram. Proven Methods
    How to find out who "liked" a person on Instagram. Proven Methods

    Everyone who is trying to start the path of a blogger or has already achieved some success in this field, hopes that the content that he shares with readers will not be left without attention. The main indicator of page popularity is “likes”. “Thumbs up” is also put when they want to attract the attention of potential subscribers

  • How to restore a channel on YouTube: detailed instructions, simple and effective tips
    How to restore a channel on YouTube: detailed instructions, simple and effective tips

    This article describes how to restore a channel on YouTube after deleting it. To understand this task, it will be useful to familiarize yourself with some terminology. There is a difference between deleting and blocking a YouTube account

  • How to remove a contact from "Vatsap" and how to block an unwanted interlocutor
    How to remove a contact from "Vatsap" and how to block an unwanted interlocutor

    WhatsApp is a popular messenger. It has many features that users are not even aware of. One of these is the ability to block contacts. It helps to get rid of annoying interlocutors. And how to remove a contact from WhatsApp and how to block it? You can perform these actions in different ways