How to quickly format a floating point number in an Android game loop?

I want to display the value of the millisecond timer in the game.

String.formaton Android is very slow, so I can not use it. I am currently using this:

long elapsedMillis = ...; //elapsed milliseconds
int whole = (int)(elapsedMillis / 1000);
int fraction = (int)(elapsedMillis % 1000);
StringBuilder sb = new StringBuilder("TIME:");
sb.append(whole);
sb.append('.');
if(fraction < 10)
sb.append("00");
else if(fraction < 100)
sb.append("0");
sb.append(fraction);
g.drawText(sb.toString(), ...);

but I think it can be done faster. Can you steer me in the right direction? Thanks


related question

+3
source share
1 answer

some ideas:

  • use static StringBuilder initialized with high capacity
  • use the built-in function
  • there is a β€œTIME:” pre-drawn (I guess it doesn't move)
  • save time in milliseconds
+3

Source: https://habr.com/ru/post/1780620/


All Articles