-2

I am trying to make a program that calculate the width or how many numbers a float contains. I need this to make good looking outputs in console. For example, the width of the following numbers are:

4     (1)

23    (2)

0.3   (3 because the . has the width of one)

0.45  (4)

12.34 (5)

0     (1)

I have tried to check if the number == 0 then the width is 1 and done, and else multiply the number with 10 until there is no decimals float == (int)float and then calculating how many times it is needed to divide by 10 to get a number float < 0. I have tried everything I have found at the internet without any real luck...

sg7
  • 6,108
  • 2
  • 32
  • 40
J. Doe
  • 335
  • 1
  • 2
  • 11
  • 6
    "Print" to a string and get the length of the string? – Some programmer dude Mar 13 '18 at 15:15
  • print to a string and count its length. or check the printf format specifiers that can do this for you. – Paul Ogilvie Mar 13 '18 at 15:15
  • 5
    Floats are not exact. So the 0.3 could also be represented as 0.2999999999999 and counting the number of digits would not be very helpful. – interjay Mar 13 '18 at 15:18
  • `printf` format specifiers are how you do this. Take a look [here](https://stackoverflow.com/questions/16413609/printf-variable-number-of-decimals-in-float). If you want fixed number of decimal places every time you can hardcode a number rather than use the `*` – yano Mar 13 '18 at 15:20
  • 1
    Is there something preventing you from using the formatting already available to you: `printf()` [see here](http://en.cppreference.com/w/c/io/fprintf)? You should be able to round, set minimum field widths, etc. – Andrew Falanga Mar 13 '18 at 15:21
  • http://floating-point-gui.de – n. m. could be an AI Mar 13 '18 at 15:22

3 Answers3

1

Just use snprintf for counting length of what you want to print, like this:

int main()
{
    float num;
    int len;
    if (1 == scanf("%f", &num)) {
        len = snprintf(NULL, 0, "%g", num);
        printf("%g (%d)\n", num, len);
    }
    return 0;
}
freestyle
  • 3,692
  • 11
  • 21
  • I just got DV for the same solution - no explanations given. I hope you will have better luck! Do not forget about `'.'` and `'-'` sign. – sg7 Mar 13 '18 at 16:10
  • @sg7 Your solution is complicated, maybe that's why you have got a DV. P.S. I didn't forget about anything! – freestyle Mar 13 '18 at 16:16
  • @freestyle I misread the requirement. My bad. I corrected my program and compared our solutions. – sg7 Mar 13 '18 at 19:14
  • @sg7 Yes, it can make sense, but why is buffer size 32? I think print to buffer is overhead for this task. If I understand correctly, author of question want to calculate max width of numbers for pretty alignment them into column, like `for (...) maxWidth = max(maxWidth, snprintf(NULL, 0, cellFormat, ...));` – freestyle Mar 13 '18 at 19:37
0

You could try to convert float to the string. You may use snprintf or something similar for your needs.

The precision of the conversion of the snprintf may not be best. You may want to limit number of characters after the '.'.

#include <stdio.h>
#include <string.h>

int main(void)
{
    float num = -12345.678; 
    char str[32];
    size_t counter = 0;

    // freestyle solution
    int len;
    printf("Please input the number:\n");
    if (1 == scanf("%f", &num)) {
        len = snprintf(NULL, 0, "%g", num);
        printf("\%g (%d)\n", num, len);
    }

    //sg7: improved one - '.' and '-' counted.
    len = snprintf(str, 32, "%f", num);
    printf("\n%s Number of characters: %zu",str, len);

    return 0;
}

Test:

Please input the number:                                                                                                                       
-12.345678                                                                                                                                     
-12.3457 (8)                                                                                                                                   

-12.345678 Number of characters: 10 
sg7
  • 6,108
  • 2
  • 32
  • 40
-1

Assuming your float is stored in the variable f, you can do this:

int width; char buffer[50];
width = sprintf(buffer,"%f",f);

Please keep in mind that floats are usually stored with a precision of six digits after the decimal point, so a number like 14.2 is stored as 14.200000 thus having a width of 9 instead of 4. It might be stored as 13.999999 as well so keep that in mind.

Ahmed Karaman
  • 523
  • 3
  • 12