/    Sign up×
Community /Pin to ProfileBookmark

formatting numbers

When I print a variable it appears on my screen like this 832873673. How can I format the variable, so I see it on my screen as: 832 873 673? (thousand seperator)

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@pyroAug 11.2003 — Use [URL=http://us2.php.net/manual/en/function.chunk-split.php]chunk_split()[/URL]

[code=php]<?PHP
$var = "832873673";
$output = chunk_split($var,"3");
echo $output;
?>[/code]
Copy linkTweet thisAlerts:
@Da_WarriahAug 11.2003 — actually according to the php.net chunk_split() function you posted, that would give:

832

873

673

because the string end defaults to rn

[code=php]$output = chunk_split($var,"3", " ");[/code]

that would do what you wanted?
Copy linkTweet thisAlerts:
@pyroAug 12.2003 — Nope, it will return exactly what they asked for...
Copy linkTweet thisAlerts:
@jerichoauthorAug 12.2003 — Thanks. I applied pyro's original code, but it doesn't do the trick.

I have a number 10000 and after echo chunk_split($variable,"3");

I got 100 00.

Not good. The space should be counted "from the other side".

How to format it right?
Copy linkTweet thisAlerts:
@Kr_ZAug 12.2003 — [code=php]
$var = 10000;
$number = number_format($var, " ", " ", " ");
[/code]
Copy linkTweet thisAlerts:
@jerichoauthorAug 12.2003 — Thanks! This works just as it should!
Copy linkTweet thisAlerts:
@jerichoauthorAug 12.2003 — OK, how about when I use float(12,2) number? How can I format it so it looks like this: 1 000 000.00?

When I apply $number = number_format($var, " ", " ", " "); the 2 decimal numbers are cut off.
Copy linkTweet thisAlerts:
@DaiWelshAug 13.2003 — Check the documentation for the function [URL=http://uk.php.net/manual/en/function.number-format.php]here[/URL] . The format you want is probably

number_format($var,2,'.',' ')

btw, the original suggestion given was not strictly correct as param 2 should be an int.

HTH,

Dai
Copy linkTweet thisAlerts:
@jerichoauthorAug 13.2003 — Thanks! That's correct.
Copy linkTweet thisAlerts:
@DaiWelshAug 13.2003 — np ?
×

Success!

Help @jericho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...