[ create a new paste ] login | about

Link: http://codepad.org/3JTWFqK0    [ raw code | output | fork ]

fisherro - C, pasted on Jan 8:
#include <stdlib.h>

float raise(float n, int times)
{
    float result = n;
    int i;
    if(times < 1) return 1;
    if(times < 2) return n;
    for(i = 1; i < times; ++i) result *= n;
    return result;
}

float my_round(float n, int places)
{
    float x = raise(10, places);
    n *= x;
    n += 0.5;
    return ((int)n)/x;
}

int main()
{
    float n = 1000.0/7.0;
    printf("%f\n", my_round(n, 3));
    printf("%f\n", my_round(n, 2));
    printf("%f\n", my_round(n, 1));
    printf("%f\n", my_round(n, 0));
    return EXIT_SUCCESS;
}


Output:
1
2
3
4
142.857000
142.860000
142.900000
143.000000


Create a new paste based on this one


Comments: