-1

I want to turn a number like 0.1235 to 1235.

i tried to do it through a loop by multiplying by 10 but i didnt know how to stop the loop.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main (){
    double a;

    printf("a:");
    scanf("%lf", &a);
    while (/* condition */)
    {
        a = a*10;
    }
    printf("a: %lf",a)
    getch ();
    return 0;

}
  • What about a number that's composed of `0.1 + 0.2` which ends up being exceedingly wonky in floating point, like `0.30000000000000004`? Are you prepared to truncate or round? – tadman Nov 11 '22 at 21:40
  • i am working with short numbers so yeah i would like to do what i said in the question – aziz khaled Nov 11 '22 at 21:43
  • 4
    @aziz khaled, `0.1235` is not the exact value of a floating point `double`. The closest encodable value is 0.12349999999999999866773237044981215149164199829101562500. What integer do your want to turn 0.12349999999999999866773237044981215149164199829101562500 into? – chux - Reinstate Monica Nov 11 '22 at 21:46
  • https://www.h-schmidt.net/FloatConverter/IEEE754.html - there is no 0.1235 – KamilCuk Nov 11 '22 at 21:46
  • 2
    Why not multiply by `10000` and remove the loop? Don't forget to round: `int n = (int)round(0.1235 * 10000);`. Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Nov 11 '22 at 21:47
  • 3
    *I want to turn a number like 0.1235 to 1235* No, you don't. It's a doubly meaningless exercise: there's no use for the result, and its' impossible, anyway. Will your results for `0.1`, `0.10000`, and `0.00001` be the same or different? – Steve Summit Nov 11 '22 at 21:49
  • i posted the code i hope you get an idea of what i am trting to do – aziz khaled Nov 11 '22 at 21:49
  • 1
    `scanf("%lf", &a);` Idea: you can store the input as a _string_ `"0.1234"` and then remove the `.` to `01234` and then convert it to an integer. Another one: you can also read `%d.%d` and then do some calculation on them. – KamilCuk Nov 11 '22 at 21:50
  • actually i want to make the number like that to reverse it later and add the "." in the right place – aziz khaled Nov 11 '22 at 21:51
  • Note that "multiply by 10000" likely incurs a rounding in forming the product. A subsequent `int` cast or round, say via `lround()` will fail edge cases as the product error may push the code result to off-by-one as compared to math. – chux - Reinstate Monica Nov 11 '22 at 21:51
  • 2
    If you want to reverse then your "number" is a string. Input a string, and reverse it. – Weather Vane Nov 11 '22 at 21:51
  • I know we have floating point here but two classic traps for new coders are assuming that "a number" is `int`, and "a character" is `char`. – Weather Vane Nov 11 '22 at 21:55
  • @aziz khaled, What is the range of possible numbers code must handle? – chux - Reinstate Monica Nov 11 '22 at 21:55
  • @chux-ReinstateMonica the same range of float numbers – aziz khaled Nov 11 '22 at 22:00

2 Answers2

0

int var = (int)round(0.1235 * 10000);

  • 1
    If you use [`lround`](https://en.cppreference.com/w/c/numeric/math/round), then you don't need the cast. – Andreas Wenzel Nov 11 '22 at 22:06
  • August Vilakia, `int` cast and `lround()` fail for a large floating point products. Apparently OP wants the [full FP range](https://stackoverflow.com/questions/74408396/how-to-make-a-float-number-an-integer-in-c?noredirect=1#comment131357046_74408396). – chux - Reinstate Monica Nov 11 '22 at 22:08
0

This is not an easy problem to solve as it may seem at first due to decimal precision and lack of implementation details in your question. Here is an attempt of a solution to your problem, but you might need to adjust it depending on your needs.

#include <stdio.h>
#include <math.h>

#define DELTA 0.00001

int get_number_of_decimal_places(double num)
{
    int count = 0;
    
    do {
        num = num * 10;
        ++count;
    } while (num - (int)num > DELTA);
    
    return count;
}

int main()
{
    double a;
    int result = 0;

    printf("a:");
    scanf("%lf", &a);
    
    int decimal_places = get_number_of_decimal_places(a);
    
    do {
        a *= 10;
        result += (int)a * pow(10, --decimal_places);
        a -= (int)a;
    } while (decimal_places != 0);
    
    printf("result: %d", result);
    getch();
    return 0;
}

For input value 0.12345, the output is:

12345

Keep in mind that this solution treats input values 0.1, 0.0001, 0.010 etc. the same way, so the output would be:

1
Aleksa Majkic
  • 632
  • 5
  • 17
  • This seems to be what the OP is looking for, but as your comments and solution indicate, the purpose is highly questionable. One detail: be careful when casting `double` to `int`. See e.g. https://stackoverflow.com/questions/24723180/c-convert-floating-point-to-int. Curiously, you need to look at the two *least* upvoted answers. – nielsen Nov 12 '22 at 07:35