J. Doe Cd
J. Doe Cd

Reputation: 69

Convert INT number in PHP

I have this number 1003636 and i need to convert in this format 1.003,636 how to convert with php, this is a dynamically numbers from rows of mysql.

Upvotes: 0

Views: 46

Answers (2)

ishegg
ishegg

Reputation: 9927

You need to use number_format(), but there's no way of telling the function that the last 3 numbers are the decimals. So you need to first divide by 1000, and then use the function:

<?php
$n = 1003636;
$n /= 1000;
echo number_format($n, 3, ",", "."); // 1.003,636 - 3 decimals, separate thousands by . and decimals by ,

Demo

Upvotes: 1

MCMXCII
MCMXCII

Reputation: 1026

PHP has a build in function, number_format. http://php.net/manual/en/function.number-format.php

E.G.

$number = 1003636;
echo number_format($number); // This would output 1,003,636

Worth checking out the documentation, as their are certain parameters you can pass for dealing with foreign numbers, e.g. dots instead of commas and vice verse.

Upvotes: 0

Related Questions