Getting each individual digit from a whole integer

后端 未结 9 1226
南方客
南方客 2020-11-30 04:05

Let\'s say I have an integer called \'score\', that looks like this:

int score = 1529587;

Now what I want to do is get each digit 1, 5, 2,

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 04:31

    Don't reinvent the wheel. C has sprintf for a reason. Since your variable is called score, I'm guessing this is for a game where you're planning to use the individual digits of the score to display the numeral glyphs as images. In this case, sprintf has convenient format modifiers that will let you zero-pad, space-pad, etc. the score to a fixed width, which you may want to use.

提交回复
热议问题