Writing a large number to shorter format

S

samyak

Guest
I want to know how to write a large number in a shorter format.
Conditions->
1> I have a variable ' Y ' increasing at a non-uniform rate
2> Let's say, if Y = 2243054, I want to write it as 2243 K +54.
3> Considering the above example, let's have arrays d[1]= 2243 and dx[1]= 54

So how to make this possible?
 
S

signal

Guest
Check out div and mod in the manual. They are the magical functions you seek.
 

TheouAegis

Member
var b,m,k,d,n;
n= variable;
if n
{
d=n mod 1000;
n = n div 1000;
}
if n
{
k = n mod 1000;
n = n div 1000;
}
if n
{
m = n mod 1000;
n = n div 1000;
}
//repeat as needed

n = "";
//Work in reverse here
if m n+=string(m)+"M ";
if k n+=string(k)+"K ";
if d n+=string(d);
return n;
 
S

samyak

Guest
var b,m,k,d,n;
n= variable;
if n
{
d=n mod 1000;
n = n div 1000;
}
if n
{
k = n mod 1000;
n = n div 1000;
}
if n
{
m = n mod 1000;
n = n div 1000;
}
//repeat as needed

n = "";
//Work in reverse here
if m n+=string(m)+"M ";
if k n+=string(k)+"K ";
if d n+=string(d);
return n;
Same statements 3 times?
 
Depending on how big your numbers can get, and how precise they need to be, you might have a problem doing the math with double precision floats.

The largest integer that you can increment by one with double precision is 2^53. And then the larger your numbers get, the less precise they become.

If this isn't a problem, then you can write the number with a simple loop:
Code:
    var _num = num;
    var _a = 0;
    var _str2;
    do {
        switch (_a++) {
            case 0: _str2 = string(floor(_num mod 1000)); break;
            case 1: _str2 = string(floor(_num mod 1000)) + " K, " + _str2; break;
            case 2: _str2 = string(floor(_num mod 1000)) + " M, " + _str2; break;
            case 3: _str2 = string(floor(_num mod 1000)) + " G, " + _str2; break;
            case 4: _str2 = string(floor(_num mod 1000)) + " T, " + _str2; break;
            case 5: _str2 = string(floor(_num mod 1000)) + " P, " + _str2; break;
            case 6: _str2 = string(floor(_num mod 1000)) + " E, " + _str2; break;
        }  
        _num /= 1000;
    } until (_num < 1);
 
Last edited:

TheouAegis

Member
Could have shortened my code using a loop, but anybody could do that. Writing it out long hand like that shows the concept.

As flying saucer said, if you are using actual decimal places you might want to floor things out first. Although really, all you need to do is take the number, get the modulo of one, and then from there everything else is already integers so you don't need to floor anything else after that. Just use div 1000 after that.

Edit: This is the same principle behind currencies in games like World Of Warcraft, but it occurred to me this is not how currency is handled in the US. Once I realized that, it became clear also that it's better to work from the top down, not the bottom up, which is what the rest of these are doing. Consider US currency.

Code:
bills[0] = 100;
bills[1] = 50;
bills[2] = 20;
bills[3] = 10;
bills[4] = 5;
bills[5] = 1;
bills[6] = 50;
bills[7] = 25;
bills[8] = 10;
bills[9] = 5;

var cash; cash[10] = 0;

var change = amount mod 1 * 100;
var dollars = amount div 1;
for(var i=0; i<10; i++;)
if i < 6
{
    cash[ i ] = dollars div bills[i];
    dollars = dollars mod bills[i];
}
else
{
    cash[i] = change div bills[i];
    change = change mod bills[i];
}
cash[10] = change;
 
Last edited:
S

samyak

Guest
Ok, I think it's working. I had forgotten about div operator.
What I'm doing is
a= Y div 1000
b= Y mod 1000
if Y>1000 { Y = 0} /* I don't want my processor to handle huge numbers,but may be use smaller numbers a and b to calculate stuff */

draw_text(x,y, string(a) + "K " + string(b));
Is this Ok?
 

TheouAegis

Member
For just a 1000 (K) and a <1000, that's fine. But I don't get the if Y>1000{Y=0} bit. If you mean to say you don't want Y to ever be over 1million, then that won't work.

b = Y mod 1000;
Y = Y div 1000;
a = Y mod 1000;
Y = 0;

That would limit it to <1million.

But... that'd set Y to 0 immediately. I don't really see a reason to set Y to 0. And if you're just going to do everything using a and b anyway, why not just break it down into a and b to begin with?

b += amount_to_add;
if b > 1000
{
var n = b div 1000;
a += n;
b -= n;
}

Probably faster to execute, too.


Edit: Your code would never let a be greater than 1. Even if you fixed it with my first suggestion but left out the Y = Y div 1000 line, then a could be 1000 (or higher) before Y gets set to 0.
 
Top