AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Digits of an Integer (https://forums.alliedmods.net/showthread.php?t=275559)

PartialCloning 12-03-2015 05:44

Digits of an Integer
 
Is it possible to format only a certain amount of digits from an integer when formatting into a string? Like you could with a float e.g "%.5f"?

Lets say the integer is 12345678. Formatting that into 4 digits should turn it into 1234. While if my number was 23, it would be formatted as 0023. I know you can do the latter by doing "%04i".

safetymoose 12-03-2015 06:00

Re: Digits of an Integer
 
PHP Code:

while(number>10000)
    
number /= 10 // same as number = number / 10 


PartialCloning 12-03-2015 06:15

Re: Digits of an Integer
 
That worked pretty well. Thank you.

fysiks 12-03-2015 20:28

Re: Digits of an Integer
 
That will only work for values between 10,000 and 99,999,999.

What exactly are you trying to do? What situation would you only want the first 4 digits?

safetymoose 12-04-2015 14:01

Re: Digits of an Integer
 
Quote:

Originally Posted by fysiks (Post 2368587)
That will only work for values between 10,000 and 99,999,999.

He mentioned that he covered the numbers below 1000 with %04i, so this is what he needed..

If he needs negative numbers below -10000, he can figure it out :D

Quote:

Originally Posted by fysiks (Post 2368587)
What exactly are you trying to do? What situation would you only want the first 4 digits?

A counter of 4 digits? maybe something to be shown on a hud message or something?

What did you need it for anyways, i'm curious to know too :)

fysiks 12-04-2015 19:52

Re: Digits of an Integer
 
Quote:

Originally Posted by safetymoose (Post 2368781)
He mentioned that he covered the numbers below 1000 with %04i, so this is what he needed..

No, he said if his number was 4 digits or less. He said "formatting into 4 digits" which is illogical for numbers with more than 4 digits. At that point it's not a format, it's actually changing the value to something else. It would be logical to say "format into at least four digits" which is what %04i does.



Quote:

Originally Posted by safetymoose (Post 2368781)
A counter of 4 digits? maybe something to be shown on a hud message or something?

Neither of those suggestions make make sense for getting the 4 left-most digits.

Bugsy 12-04-2015 21:14

Re: Digits of an Integer
 
Quote:

Originally Posted by PartialCloning (Post 2368378)
Lets say the integer is 12345678. Formatting that into 4 digits should turn it into 1234. While if my number was 23, it would be formatted as 0023. I know you can do the latter by doing "%04i".

Quote:

Originally Posted by fysiks (Post 2368862)
No, he said if his number was 4 digits or less. He said "formatting into 4 digits" which is illogical for numbers with more than 4 digits. At that point it's not a format, it's actually changing the value to something else. It would be logical to say "format into at least four digits" which is what %04i does.

Neither of those suggestions make make sense for getting the 4 left-most digits.

What safetymoose provided does what the OP asked for, fysiks, regardless of if it makes logical sense to do so. I am also curious why you would want the left-most 4 digits.
PHP Code:

new iVal 12345678
    
while(iVal>10000)
    
iVal /= 10 
        
server_print
"%04d" iVal );
    
iVal 23
while(iVal>10000)
    
iVal /= 10 

server_print
"%04d" iVal ); 

Output
Code:

1234
0023


PartialCloning 12-05-2015 21:21

Re: Digits of an Integer
 
Quote:

Originally Posted by fysiks (Post 2368587)
That will only work for values between 10,000 and 99,999,999.

What exactly are you trying to do? What situation would you only want the first 4 digits?

It worked for all the values I threw at it. I did have to adjust the zeros in the while check accordingly.

I needed to convert get_gametime() seconds to miliseconds and I found this post:

Quote:

Originally Posted by SchlumPF* (Post 782132)
this might help:

PHP Code:

public fnClimbtimeToStringFloat:climbtimeoutput[], len )
{
    new 
minutessecondsmiliseconds;
    
    
minutes floatroundclimbtime 60.0floatround_floor );
    
seconds floatroundclimbtime minutes 60floatround_floor );
    
miliseconds floatround( ( climbtime - ( minutes 60 seconds ) ) * 1000000floatround_floor );
    
    
formatoutputlen"%02i:%02i:%06i"minutessecondsmiliseconds );



I only wanted the first few digits of the miliseconds. But then I found out that miliseconds are actually seconds * 1000.

I'm still glad I learned the method you guys posted, I'm sure it'll come in handy one day.

fysiks 12-06-2015 21:59

Re: Digits of an Integer
 
Quote:

Originally Posted by PartialCloning (Post 2369197)
I'm still glad I learned the method you guys posted, I'm sure it'll come in handy one day.

Taking the 4 left-most digits is nonsensical logic and is therefore invalid (I can't think of any valid use).

If you need to convert between seconds and milliseconds, either multiply by 1000 to go from seconds to milliseconds or divide by 1000 to go from milliseconds to seconds (when dividing, make sure both numbers are floating point values). But, it sounds like you figured this part out.

So, what happened in this thread is known as the XY Problem. To avoid this in the future, when asking questions, always explain what you are actually trying to do.

PartialCloning 12-06-2015 22:34

Re: Digits of an Integer
 
I would actually prefer to get an answer to my questions as once you have a certain question you should always try to find an answer for it.

I'm sure taking only certain digits can be useful in certain situations. One that I can think of is showing a player's money in thousands. E.g $100216 -> $100k. Obviously you would need to adjust it to make sure it doesn't show 1234 as 123k and so on.

I don't consider it a waste of time as it says in that XY problem page. I had a question and I got an answer and learned something, I'm thankful to those that answered.

But lol @:
Quote:

Originally Posted by http://xyproblem.info/
Example 1

n00b doesn't actually want the last 3 chracters in a filename, he wants the file extensions, so why ask for the last 3 characters?



All times are GMT -4. The time now is 17:52.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.