-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atof.c
56 lines (51 loc) · 1.48 KB
/
ft_atof.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_atof.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/10 12:05:39 by jvan-hal #+# #+# */
/* Updated: 2024/01/11 20:07:43 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
int checksign(char **str)
{
int sign;
sign = 1;
if (**str == '+' || **str == '-')
{
if (**str == '-')
sign = -1;
++(*str);
}
return (sign);
}
double ft_atof(char *str)
{
double val;
int sign;
int significant;
val = 0;
sign = checksign(&str);
significant = 0;
while (*str && (ft_isdigit(*str) || *str == '.'))
{
if (*str != '.')
{
val = (val * 10) + (*str - '0');
significant *= 10;
}
else if (*str == '.')
{
if (significant > 0)
return (0);
significant = 1;
}
++str;
}
if (val > 0 && significant > 0)
val /= significant;
return (val * sign);
}