November 08, 2026, 04:21:25 pm

Author Topic: Number format  (Read 134 times)

0 Members and 1 Guest are viewing this topic.

Offline Red Wolf

Number format
« on: January 07, 2016, 09:12:34 pm »
Hi,
I got this piece of code which gets id from a field.
Code: [Select]
<?php
    $game 
= edited_connector_get_game(get_field('game_id'));
    echo 
"Game voted from " . $game["gamevotes"] . " users." ;
?>
basically, the output is
Game voted from X users, where X is a number.
How can i make this number in English format, aka, add  comma after 3 digits.
Now i get:
Game voted from 1234 users,
i want it to be
Game voted from 1,234 users.
« Last Edit: January 08, 2016, 06:26:27 am by Red Wolf »
Life can only be understood backwards but it must be lived forwards. -Soren Kierkegaard




Offline 'PL'

Re: Number format
« Reply #1 on: January 08, 2016, 12:49:42 am »
where does the php read the data?

I think you can separate the data in 2 varibles and place a echo "," between them.

one of the variables will acquire the numbers greater than 1000 and change accordingly to the data it reads, and the other 3 numbers can be placed in another variable.

I have't worked with php at all so I'm following just practical instinct :)

Offline TeeKaaY

Re: Number format
« Reply #2 on: January 08, 2016, 01:01:05 am »
If I remember correctly there is a function that you can use to do what you want it to do. The function in question is number_format( ). Simply change your code to this:
Code: [Select]
    $game = edited_connector_get_game(get_field(game_id'));
    echo "Game voted from " . number_format( $game["gamevotes"] ) . " users." ;

Offline Red Wolf

Re: Number format
« Reply #3 on: January 08, 2016, 07:19:53 pm »
where does the php read the data?

I think you can separate the data in 2 varibles and place a echo "," between them.

one of the variables will acquire the numbers greater than 1000 and change accordingly to the data it reads, and the other 3 numbers can be placed in another variable.

I have't worked with php at all so I'm following just practical instinct :)

Its a wordpress page and there is a field, the data is read there, a plugin takes the ID data from an external site, and I use strings on manual to get datas I need according to the 1st field mentioned.

If I remember correctly there is a function that you can use to do what you want it to do. The function in question is number_format( ). Simply change your code to this:
Code: [Select]
    $game = edited_connector_get_game(get_field(game_id'));
    echo "Game voted from " . number_format( $game["gamevotes"] ) . " users." ;
Thanks a lot man, that worked.
U can lock this.
Life can only be understood backwards but it must be lived forwards. -Soren Kierkegaard




Offline Rawrplus

Re: Number format
« Reply #4 on: January 15, 2016, 08:52:19 am »
Alternatively, you can transform the data to an array of strings and make an if condition, that when a string (in this case our number) consits of more or is equal to 4 characters,  you set the second field to comma add print out the rest, although much more practical is to use the actual function tony provided. This is just a other-way-around solution you could use in programming languages that do not provide such function.

Here's a graphical illustration of how it would work for easier comprehension

Code: [Select]
if a number <= 4 characters
   print out the number
else
   make every character of the number into an array
   for example the standard input would be 1258
       set the 0th string to 1st character which would be 1 second to comma etc
       in this case
       --------------------
       [0][1][2][3][4]
        1  .  2  5  8 
       --------------------
        Doesn't really matter how large the number would be, you can create a for loop that keeps on extending the array until we use all the characters from input.
       Important is only adding the first number and the comma.
       It's a pretty stupid/binary solution, but I didn't think of anything better that could be universally applied to pretty much any programming language. Obviously there are easier ways to do it, without even having to do this, but this is like easiest way to do it that would work with pretty much everything.


Though yeah, use the function if you can, no need to complicate our lives.
« Last Edit: January 15, 2016, 09:03:05 am by Rawrplus »


[ 15% ] Finally update Billing Section once and for all
[ 50% ] Ambrosia Garry's Mod Re-Invented
[ 10% ] My web-coding online series
[ 20% ] Brand new forums
[ 30% ] Ambrosia FAQ
[ 10% ] Ambrosia TV
      
           
              [ ► ] Ambrosia History (2009-2014)
              [ ► ] Ambrosia Official Twitter
              [ ► ] Ambrosia Steam Group
              [ ► ] Ambrosia Facebook
              [ ► ] Support Section
              [ ► ] Site RSS
       
             
               [ ► ] Contact me through Steam
               [ ► ] Portfolio (Unfinished)
               [ ► ] Ambrosia Teamspeak
               [ ► ] Send me a PM






[info]I am currently inactive, please contact other highstaff if you need anything[/info]