-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.c
More file actions
39 lines (32 loc) · 735 Bytes
/
float.c
File metadata and controls
39 lines (32 loc) · 735 Bytes
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
#include "float.h"
#include "integer.h"
int print_decimal_places_f(float num) {
int decimal_places = 0;
while ((int) num != num) {
num *= 10;
decimal_places++;
}
return (decimal_places);
}
void print_float(float f, int precision)
{
int end = print_decimal_places_f(f);
if (precision == 6) {
precision = end;
} else {
end = precision;
}
if (f < 0) {
print_character('-');
f = -f;
}
print_integer((int)f);
print_character('.');
f -= (int)f;
for (int i = 0; i < precision && end > 0; i++) {
f *= 10;
print_integer((int)f);
f -= (int)f;
end--;
}
}