norming & cleaning ft_putnbr & ft_putnbr_fd

This commit is contained in:
Tanguy MAZE 2018-04-08 15:43:05 +02:00
parent 0ecdc73ca9
commit 8c2756952c
2 changed files with 27 additions and 5 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 12:52:53 by tmaze #+# #+# */
/* Updated: 2018/04/08 12:54:35 by tmaze ### ########.fr */
/* Updated: 2018/04/08 15:33:50 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,5 +14,5 @@
void ft_putnbr(int n)
{
ft_putstr(ft_itoa(n));
ft_putnbr_fd(n, 1);
}

View File

@ -6,13 +6,35 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 13:05:24 by tmaze #+# #+# */
/* Updated: 2018/04/08 13:06:15 by tmaze ### ########.fr */
/* Updated: 2018/04/08 15:41:23 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static unsigned long ft_abs(int n)
{
if (n < 0)
return ((unsigned long)n * -1);
return ((unsigned long)n);
}
void ft_putnbr_fd(int n, int fd)
{
ft_putstr_fd(ft_itoa(n), fd);
unsigned long mult;
int nb_nbr;
unsigned long nbr;
mult = 10;
nbr = ft_abs(n);
while (mult < nbr)
mult *= 10;
if (n < 0)
write(fd, "-", 1);
while (mult > 1)
{
nb_nbr = '0' + ((nbr % mult) / (mult / 10));
write(fd, &nb_nbr, 1);
mult /= 10;
}
}