Libft extra functions
This commit is contained in:
38
lib/libft/ft_atof.c
Normal file
38
lib/libft/ft_atof.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/09/22 20:46:43 by aortigos #+# #+# */
|
||||
/* Updated: 2023/10/18 20:41:03 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
double ft_atof(char *str)
|
||||
{
|
||||
int num;
|
||||
double rest;
|
||||
char *tmp;
|
||||
int i;
|
||||
|
||||
num = ft_atoi(str);
|
||||
tmp = ft_strchr(str, '.');
|
||||
if (!tmp)
|
||||
tmp = ft_strchr(str, ',');
|
||||
if (!tmp)
|
||||
return (num);
|
||||
rest = ft_atoi(&tmp[1]);
|
||||
i = 1;
|
||||
while (tmp[i])
|
||||
{
|
||||
rest /= 10;
|
||||
i++;
|
||||
}
|
||||
if (num < 0 || (str[0] == '-' && str[1] == '0'))
|
||||
return (num - rest);
|
||||
return (num + rest);
|
||||
}
|
||||
Reference in New Issue
Block a user