3

He conseguido separar la parte entera y decimal de un número double en c, pero quiero convertir la parte decimal a entero, ejemplo:

numero = 123.34
parte entera = 123
parte decimal = 34

y lo que entiendo hasta ahora es que para convertirla necesito multiplicar la parte decimal por 10, ejemplo:

.34 * 10 = 3.4
3.4 * 10 = 34

pero, ¿cómo puedo detener el bucle while en mi código?:

#include <stdio.h>
#include <windows.h>
int main(){

    double numero_flotante;
    int entero_numero_flotante;

    printf("Ingresa tu numero: ");
    scanf("%lf", &numero_flotante);
    entero_numero_flotante = numero_flotante; //SACAR EL NUMERO ENTERO DEL FLOTANTE
    numero_flotante = numero_flotante - entero_numero_flotante;//SACAR LA PARTE DECIMAL DEL FLOTANTE

    printf("parte entera: %d\n", entero_numero_flotante);
    printf("parte decimal: %lf\n\n", numero_flotante);

    while(1){
        printf("%lf\n", numero_flotante);
        numero_flotante = numero_flotante * 10;
        Sleep(2000);
    }
}
0

2 respuestas 2

3

El bucle while se detendra depende al valor de n, el numero n debe ir disminuyendo y condicionado a que sea mayor a 0.

int numeroConvertido=0;
while(n>0){
        numeroConvertido+= (n%10) * 10;
        n=n/10;
    }
0
1

Lo resolví con la respuesta de x-rw. Publico el código completo por si a alguien le sirve:

#include <stdio.h>
#include <windows.h>
int main(){

    double numero_flotante;
    int entero_numero_flotante ,n;
    printf("Ingresa tu numero: ");
    scanf("%lf", &numero_flotante);
    entero_numero_flotante = numero_flotante; //SACAR EL NUMERO ENTERO DEL FLOTANTE
    numero_flotante = numero_flotante - entero_numero_flotante;//SACAR LA PARTE DECIMAL DEL FLOTANTE

    printf("parte entera: %d\n", entero_numero_flotante);
    printf("parte decimal: %lf\n\n", numero_flotante);

    while(n > 0){

        printf("%lf\n", numero_flotante);
        numero_flotante = numero_flotante * 10;
        n = numero_flotante;
        numero_flotante = numero_flotante -n;
        printf("%d\n", n);

      }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.