So I'm working on a port of Cookie Clicker, which of course has huge variables, so I decided to use floats assuming nobody will bother reaching 3.4 undecillion. I, of course, do not want to render 38 digit floats several times on screen so how would I go about making it shorter?
Example: instead of displaying "1000000 cookies" it would say "1.000 million cookies"
Example 2: instead of displaying "2342962531 cookies" it would say "2.343 billion cookies"
Any ideas? I also need to print this out using the graphx.h gfx_Print functions so it would need to be an int or a string. Thanks in advance guys!
MateoConLechuga wrote:
So using that library, how would I go about doing the thing in the post above?
I ended up trying it and I couldn't figure out how to implement it since my program uses decimal, unless you have any ideas I think I'm stuck with floats, which shouldn't be too big of a deal I guess
MateoConLechuga wrote:
Floats are perfectly adequate for this use case.
If you wanted to have a precision of single cookies even into the quintillions then yes, a bigint library would be better, but realistically nobody is even going to notice one cookie going missing once you hit thousands of cookies per second.
A simple (if somewhat inefficient) algorithm for doing this would be something like this:
Code: suffixes = "", "thousand", "million", "billion", "trillion", ...
x = numCookies
for suffix in suffixes:
if x < 1000:
print(round(x, 3), suffix)
break
x /= 1000
As for how to print a float in C, you would normally use sprintf, but since the toolchain's printf is rather large and the OS's printf doesn't handle floats correctly, you can get the integer and floating point parts separately and then print them:
Code: int ipart = (int)x;
int fpart = ((int)(1000*x)) % 1000;
gfx_PrintUInt(ipart, 1);
gfx_PrintChar('.');
gfx_PrintUInt(fpart, 3);
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum