Advertisement

Extracting place values

Started by July 29, 2000 09:54 AM
4 comments, last by t2sherm 24 years, 4 months ago
I have an integer than has a number like, 5349, and I want to get the value of each place, thousands, hundreds, tens, and ones. I can get the thousands place by diviing 5349 by 1000 and getting 5.349, and then i get 5. But i can''t figure out how to get the 3, 4 or 9 for the other places. Its probably something easy, but i can''t seem to figure it out right now. Thanks for any help. t2sherm ô¿ô
t2sherm ô¿ô
5349

thousands = 5349 / 1000 = 5
hunderds = 5349 / 100 - thousands * 10 = 53 - 50 = 3
decimals = 5349 / 10 - thousands * 100 - hundreds * 10 - 534 - 500 - 30 = 4

units = 5349 - thousands * 1000 - hunderds * 100 - tens * 10 = 5349 - 5000 - 300 - 40 = 9




get it?

Edited by - SkatMan on July 29, 2000 11:37:17 AM
Advertisement
To get the last digit of any number, just mod that number by 10

onesPlace = number % 10

It''s fairly easy to get the other places as well, just divide by a multiple of 10 after modding by the needed value (hundreds, thousands, etc... for instance:

tensPlace = (number % 100) / 10

hundredsPlace = (number % 1000) / 100

Also, in your example, it only works like you want it to if you assume that the number is 4 digits long... don''t assume that after truncating, you''ll get a single digit value: (e.g.)

15349 / 1000 = 15.349 = (truncated) 15 (not the thousand''s place!)

349 / 1000 = .349 = (truncated) 0 (not what you want!)


Can I ask what you''re using this code for, out of curiosity?

Monday is an awful way to spend one seventh of your life.
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
To get the 100th place: 5349/100 = 53 and then 53%10 = 3

To get the 10th place: 5349/10 = 534 and then 534%10 = 4

To get the One place: 5349%10 = 9

Incase you are wonder, the % sign stands for modulo, it returns the remainder of the division, so 7%5 would return 2.

Another thing is , if you are using pascal, u have to typ 7 mod 2

hope this helps
Wow, thanks for all the replies!

quote:
Can I ask what you''re using this code for, out of curiosity?


I am making a game (in C++ w/ DirectX 7), and I want to write out the score, and right now i am just converting the integer to an array of chars, and then writing it out w/ the GDI, and besides being terribly slow, the score doesn''t look good, so I''m going to make the digits 0 - 9 in photoshop, and then extract the different place values, and blit the correct combination of numbers so it prints the score.

t2sherm ô¿ô
t2sherm ô¿ô
Ummm... there are two easier solutions to your problem.

There''s a function called itoa() that will convert an integer to an array of characters.

Also, there''s a function called sprintf() that will bring any combination of basic types in and build a string out of them.

Just a suggestion, I know it''s possible that you already know about these and just wanted to write your own, but if you didn''t know, then now you do

This topic is closed to new replies.

Advertisement