-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap_utils.c
More file actions
57 lines (50 loc) · 1.64 KB
/
Copy pathpush_swap_utils.c
File metadata and controls
57 lines (50 loc) · 1.64 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/24 09:14:12 by miissa #+# #+# */
/* Updated: 2026/01/03 13:00:30 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void put_sign(double *val, int fd)
{
if (*val < 0)
{
ft_putchar_fd('-', fd);
*val = -(*val);
}
}
static void split_parts(double val, long *whole, long *frac)
{
*whole = (long)val;
*frac = (long)((val - (double)(*whole)) * 100.0 + 0.5);
if (*frac == 100)
{
(*whole)++;
*frac = 0;
}
}
static void put_two_digits(long n, int fd)
{
ft_putchar_fd((char)('0' + ((n / 10) % 10)), fd);
ft_putchar_fd((char)('0' + (n % 10)), fd);
}
void ps_putdouble2_fd(double val, int fd)
{
long whole;
long frac;
put_sign(&val, fd);
split_parts(val, &whole, &frac);
ft_putnbr_fd((int)whole, fd);
ft_putchar_fd('.', fd);
put_two_digits(frac, fd);
}
void ps_putpercent2_fd(double val, int fd)
{
ps_putdouble2_fd(val, fd);
ft_putchar_fd('%', fd);
}