Obtaining Rest of a Floating Point

3

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?

    
asked by anonymous 07.02.2016 / 23:00

4 answers

3

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

    
10.02.2016 / 04:21
2
#include <math.h>

double x = 1.13;
double resto = floor((x - floor(x)) * 100);
//                    ^^^^^^^^^^^^ <== 0.13
    
07.02.2016 / 23:26
2

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

    
08.02.2016 / 01:40
0
    #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
}
    
20.05.2018 / 21:18