#I can't make this round function work.

10 messages · Page 1 of 1 (latest)

quasi surge
#

Hey, so, i wanted a function that rounds everything to a multiple of 50 upwards, tried first with floatround, well, it did not work, then did it with string and worked perfecty with numbers within 1 - 1.000 and i don't know why but it works brilliant with numbers above 10.000, but when i try to round 1444 and 1877 it throws a crashdetect error.

i put this code in ongamemodeinit to test it

printf("redondear 89");
    temp = redondear_precio(89);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");
    printf("redondear 123");
    temp = redondear_precio(123);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");
    printf("redondear 789");
    temp = redondear_precio(789);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");
    printf("redondear 1444");
    temp = redondear_precio(1444);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");
    printf("redondear 1877");
    temp = redondear_precio(1877);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");    
    printf("redondear 10489");
    temp = redondear_precio(10489);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");
    printf("redondear 16720");
    temp = redondear_precio(16720);    
    printf("redondeado quedo en %d", temp);
    printf("------------------------------------------");

i put the code and the rest of the quetion below

#

this is the console output

[10:18:34] [Info] tmp = 89
[10:18:34] [Info] aca no deberia entrar
[10:18:34] [Info] redondeado quedo en 90
[10:18:34] [Info] ------------------------------------------
[10:18:34] [Info] redondear 123
[10:18:34] [Info] tmp = 23
[10:18:34] [Info] aca deberia entrar si es menor a 50
[10:18:34] [Info] numero termina siendo 150
[10:18:34] [Info] redondeado quedo en 150
[10:18:34] [Info] ------------------------------------------
[10:18:34] [Info] redondear 789
[10:18:34] [Info] tmp = 89
[10:18:34] [Info] aca deberia entrar si es mayor a 50
[10:18:34] [Info] tira 1
[10:18:34] [Info] numero termina siendo 800
[10:18:34] [Info] redondeado quedo en 800
[10:18:34] [Info] ------------------------------------------
[10:18:34] [Info] redondear 1444
[10:18:34] [Info] tmp = 44
[10:18:34] [Info] aca deberia entrar si es menor a 50
[10:18:34] [Info] numero termina siendo 1450
[10:18:34] [Info] redondeado quedo en 0
[10:18:34] [Info] ------------------------------------------
[10:18:34] [Info] redondear 1877
[10:18:34] [Info] tmp = 77
[10:18:34] [Info] aca deberia entrar si es mayor a 50
[10:18:34] [Info] tira 1
[10:18:34] [Info] numero termina siendo 1900
[10:18:34] [Info] redondeado quedo en 0
[10:18:34] [Info] ------------------------------------------
[10:18:34] [Info] redondear 10489
[10:18:35] [Info] tmp = 89
[10:18:35] [Info] aca deberia entrar si es mayor a 50
[10:18:35] [Info] tira 1
[10:18:35] [Info] numero termina siendo 10500
[10:18:35] [Info]
----------------------------------```` 

this is the code ( i tried making it return in every if to see if something changes but it does not, and i left it like that)
#
redondear_precio(numero)
{
    new laburo[4], tmp, slot_para_2_digitos;
    valstr(laburo, numero);
    if(10<=numero < 100) slot_para_2_digitos = 0;
    if(100<=numero < 1000) slot_para_2_digitos = 1;
    if(1000<=numero < 10000) slot_para_2_digitos = 2;
    if(10000 <= numero < 100000) slot_para_2_digitos = 3;
    tmp = strval(laburo[slot_para_2_digitos]);
    strdel(laburo, 0, sizeof laburo);
    printf("tmp = %d", tmp);

    if(0 < tmp < 50) 
    {
        printf("aca deberia entrar si es menor a 50");
        numero = numero-tmp+50;
        printf("numero termina siendo %d", numero);            
        return numero;
    }
    else if(99 > tmp > 50)
    {
        if(numero < 100)
        {
            printf("aca no deberia entrar");
            float(numero);
            tmp = numero/10;
            floatround(tmp);
            numero = 10+(tmp*10);            
            return numero;
        }
        else if(numero >= 100)
        {
            printf("aca deberia entrar si es mayor a 50");
            if(tmp > 50)
            {
                printf("tira 1");
                numero = numero-tmp+100;
                printf("numero termina siendo %d", numero);                
                return numero;
            }
            else if(tmp < 50)
            {
                printf("tira 2");
                numero = numero-tmp+50;    
                printf("numero termina siendo %d", numero);                
                return numero;        
            }                    
        }        
    }
    printf("retorno extranio");
    return numero;
}```
halcyon cliff
#

i don't know if I understood correctly, but isn't that what you're trying to achieve?

redondear_precio(numero)
{
    if (numero % 50 == 0)
    {
        return numero;
    }
    return (numero / 50 + 1) * 50;
}
for (new i, number; i < 20; i ++)
{
    number = random(11000);
    printf("%d | %d", number, redondear_precio(number));
}

output:

30 | 50
6445 | 6450
2017 | 2050
1802 | 1850
2490 | 2500
8493 | 8500
2400 | 2400
6042 | 6050
1785 | 1800
908 | 950
8818 | 8850
10003 | 10050
10218 | 10250
7078 | 7100
2825 | 2850
4266 | 4300
5266 | 5300
10473 | 10500
4211 | 4250
7886 | 7900
quasi surge
halcyon cliff
#

sure

quasi surge
#

for numbers less that 100, that it rounds to multiples of 10

#

i saw that solution a while ago but someone said it did not work for it to round upwards and did not consider it, and then wow, there it is

halcyon cliff
#
redondear_precio(numero)
{
    if (numero < 100)
    {
        if (numero % 10 == 0)
        {
            return numero;
        }
        return (numero / 10 + 1) * 10;
    }
    if (numero % 50 == 0)
    {
        return numero;
    }
    return (numero / 50 + 1) * 50;
}

probably it could be shortened somehow

quasi surge
#

i'm still amazed at how simple it was and how i complicated myself so much that ended with that thing that i did, god

THANK YOU, saved my morning dude