I have a question about this.
Let's say I have a floating point, with a value of 1.13 (Minutes.Seconds) How do I get the rest (13) of the floating point?
Minutes. Seconds (Rest) 1 . 13
How do I get the floating point 13?
I have a question about this.
Let's say I have a floating point, with a value of 1.13 (Minutes.Seconds) How do I get the rest (13) of the floating point?
Minutes. Seconds (Rest) 1 . 13
How do I get the floating point 13?
You can do a cast to get the integer value of 1.13
which is 1
, then just do this equation: partedecimal = valordouble - parteinteira
to get the 0.13
value, , its decimal part.
See the adaptation:
#include <stdio.h>
double obter_parte_decimal(double);
int main(void)
{
printf("\nParte decimal de 1.13 = %f", obter_parte_decimal(1.13)); /*O valor que vc deseja obter.*/
printf("\nParte decimal de 1.23 = %f", obter_parte_decimal(1.23));
printf("\nParte decimal de 2.19 = %f", obter_parte_decimal(2.19));
return 0;
}
double obter_parte_decimal(double valor)
{
return valor - (int)valor;
}
Output:
Decimal part of 1.13 = 0.130000
Decimal part of 1.23 = 0.230000
Decimal part of 2.19 = 0.190000
I created the obter_parte_decimal
function to reach the goal and return the decimal part, whose signature is double obter_parte_decimal(double);
in this way you can get the best out of it.
Source: link
#include <math.h>
double x = 1.13;
double resto = floor((x - floor(x)) * 100);
// ^^^^^^^^^^^^ <== 0.13
Three ways:
#include <math.h>
double a=1.13;
// -> Forma 1
double f1 = a - ((long) a);
// -> Forma 2
double temp;
double f2 = modf(a, &temp);
// -> Forma 3
double f3 = remainder(a, 1.0);
After running:
f1 = 0.130000
f2 = 0.130000
f3 = 0.130000
Multiplying the result by 100.0, for example, you get the 13.
source: Extract fractional part of double efficiently in C
#include <stdio.h>
/*Programa que separa as partes real e inteira de um valor real.*/
int main()
{
float num = 4.13;
float parteDec = 0;
int parteInt = 0;
parteInt = num;
parteDec = num - parteInt;
printf("Parte inteira %i\n",parteInt); //Saída = 4
printf("Parte decimal %g\n",parteDec); //Saída = 0.13
}