48 lines
617 B
C
48 lines
617 B
C
|
#include <stddef.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdlib.h>
|
||
|
#include "utils.h"
|
||
|
|
||
|
static size_t get_size(size_t num)
|
||
|
{
|
||
|
size_t nb_digit;
|
||
|
size_t rest;
|
||
|
|
||
|
nb_digit = 0;
|
||
|
if (num == 0)
|
||
|
nb_digit++;
|
||
|
rest = num;
|
||
|
while (rest != 0)
|
||
|
{
|
||
|
nb_digit++;
|
||
|
rest = rest / 10;
|
||
|
}
|
||
|
return (nb_digit);
|
||
|
}
|
||
|
|
||
|
static void ft_putdigit(size_t num)
|
||
|
{
|
||
|
if (num > 9)
|
||
|
{
|
||
|
ft_putdigit(num / 10);
|
||
|
ft_putchar(num % 10 + 48);
|
||
|
}
|
||
|
else
|
||
|
ft_putchar(num + 48);
|
||
|
}
|
||
|
|
||
|
void ft_putnum(size_t num, size_t padding)
|
||
|
{
|
||
|
size_t size;
|
||
|
size_t i;
|
||
|
|
||
|
i = 0;
|
||
|
size = get_size(num);
|
||
|
while (i < padding - size)
|
||
|
{
|
||
|
ft_putchar('0');
|
||
|
i++;
|
||
|
}
|
||
|
ft_putdigit(num);
|
||
|
}
|