-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printfnum.c
71 lines (64 loc) · 1.6 KB
/
ft_printfnum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printfnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: evdos-sa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/23 16:57:01 by evdos-sa #+# #+# */
/* Updated: 2023/01/21 12:35:11 by evdos-sa ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_nbrlen(long nbr, int base)
{
int i;
i = 1;
if (nbr < 0)
{
i++;
nbr = -nbr;
}
while (nbr >= base)
{
nbr /= base;
i++;
}
return (i);
}
int ft_putnbr_fd(int n, int fd)
{
int nbr;
nbr = n;
if (n >= 0)
{
if (n > 9)
ft_putnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + 48, fd);
}
else if (n == -2147483648)
ft_putstr_fd("-2147483648", fd);
else
{
ft_putchar_fd('-', fd);
ft_putnbr_fd(-n, fd);
}
return (ft_nbrlen(nbr, 10));
}
size_t ft_uputnbr_fd(size_t n, int fd)
{
size_t nbr;
nbr = n;
if (n >= 0)
{
if (n > 9)
ft_uputnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + 48, fd);
}
else
{
ft_putchar_fd('-', fd);
ft_uputnbr_fd(-n, fd);
}
return (ft_nbrlen(nbr, 10));
}