Raised This Month: $ Target: $400
 0% 

Digits of an Integer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-03-2015 , 05:44   Digits of an Integer
Reply With Quote #1

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".
PartialCloning is offline
safetymoose
Senior Member
Join Date: Feb 2015
Old 12-03-2015 , 06:00   Re: Digits of an Integer
Reply With Quote #2

PHP Code:
while(number>10000)
    
number /= 10 // same as number = number / 10 

Last edited by safetymoose; 12-03-2015 at 06:01.
safetymoose is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-03-2015 , 06:15   Re: Digits of an Integer
Reply With Quote #3

That worked pretty well. Thank you.
PartialCloning is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-03-2015 , 20:28   Re: Digits of an Integer
Reply With Quote #4

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?
__________________
fysiks is offline
safetymoose
Senior Member
Join Date: Feb 2015
Old 12-04-2015 , 14:01   Re: Digits of an Integer
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
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

Quote:
Originally Posted by fysiks View Post
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

Last edited by safetymoose; 12-04-2015 at 14:02.
safetymoose is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-04-2015 , 19:52   Re: Digits of an Integer
Reply With Quote #6

Quote:
Originally Posted by safetymoose View Post
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 View Post
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.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-04-2015 , 21:14   Re: Digits of an Integer
Reply With Quote #7

Quote:
Originally Posted by PartialCloning View Post
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 View Post
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
__________________

Last edited by Bugsy; 12-04-2015 at 21:15.
Bugsy is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-05-2015 , 21:21   Re: Digits of an Integer
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
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* View Post
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.
PartialCloning is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-06-2015 , 21:59   Re: Digits of an Integer
Reply With Quote #9

Quote:
Originally Posted by PartialCloning View Post
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.
__________________

Last edited by fysiks; 12-06-2015 at 22:01.
fysiks is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-06-2015 , 22:34   Re: Digits of an Integer
Reply With Quote #10

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?
PartialCloning is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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