PHP Equals to 1000 Show 1

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Hlo
so i've been helping a friend and i got stuck somewhere.

What I want to actually do is if $user->points equals to 1000 it should show 1 at somewhere.
Code I tried:
PHP:
<?php if($user->points == 1000) { echo "1"; }?>

This is pain. I know. This is pain as fuck. Like i know there is somewhat better way to do this but I don't know what.
I hope all those makes sense.
ty
berk aka bitch
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Need to post rest of code buddy. Need to tell us what's occurring instead of it displaying '1'.
As you can see in code It's only working if $user->points equals to 1000. I want it to rest numbers like 2k 3k 4k 5k and so. I have nothing...
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Just do
PHP:
// Round number to nearest thousand
$rounded_number = round($original_number, -3);

// Print the first character of the rounded number
echo substr($rounded_number, 0, 1);
Works somewhat, but it shows 1 when its 100. I need to make it 1000
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
  1. You're really confusing.
  2. You clearly wrote you wanted to round up to the nearest thousand, which means it'll print 1 if the number is 100.
I'm really sorry then. So lets start over again. make it so it will show 1,5 if its 1500 and so. (It's customer so I'm really sorry about these mind-chaning things..)
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
I'm really sorry then. So lets start over again. make it so it will show 1,5 if its 1500 and so. (It's customer so I'm really sorry about these mind-chaning things..)
Or you could just research like the rest of us does, instead of getting people to do your work for you, which then you'll never learn how to do it yourself.
Sorry if I seem harsh, I'm just being logical and realistic.

I don't think there's any code issues that hasn't been posted on Stackoverflow.
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Or you could just research like the rest of us does, instead of getting people to do your work for you, which then you'll never learn how to do it yourself.
Sorry if I seem harsh, I'm just being logical and realistic.
Thanks for the input.

So I've fixed this by soooo easy way:
PHP:
<?php 
echo $user->points / 1000; 
?>
I'm really sorry about this.
 

Brad

Well-Known Member
Jun 5, 2012
2,320
993
My guess is this is for the Topstats page.
PHP:
<?php function digit_format($_num) {
            if (!is_numeric($_num)) {
                return '0';
            }
            if ($_num < 1000) {
                return $_num;
            }
            $_array = array(1000000000000  =>  'B',
                          1000000000  =>  'B',
                             1000000  =>  'M',
                                1000  =>  'K'
                        );
            foreach ($_array as $_integer=> $_str) {
                $_value = $_num / $_integer;
                if ($_value >= 1) {
                    $_round = round($_value);
                    return $_round . '' . $_str;
                }
            }
        } ?>

Pace that somewhere in the file, then use it like this.
PHP:
<?php echo digit_format($users->credits); ?>
 

Users who are viewing this thread

Top