60 lines
1.5 KiB
C
60 lines
1.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* putnum.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/04/27 11:28:00 by cchauvet #+# #+# */
|
|
/* Updated: 2023/04/27 11:28:01 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#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);
|
|
}
|