1

I want to express the final product rounded down according to scientific notation. So if 80.55 and 879.5689 give 70849.281250 the final print functions outputs 70849.28.

Is there a way to get the number of decimal places of two entered floats? (I get that technically they don't have any decimal places but is it possible?) If I have two integers declared as the number of decimal places of each, I could compare using if/else and get the least one declared as another integer, but again I don't know how to use an int in place of 5 here: %.5f

kaylum
  • 13,833
  • 2
  • 22
  • 31
  • 6
    If you enter the values with `fgets` you can find the position of the decimal point (if any) with `strchr()` and calculate the number of decimal places that were entered from that and the string length. Then use `sscanf()` or `strtof()` to extract the value. Don't forget to [remove trailing newline character from `fgets()` input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). Finally if you can move forwards from the 20th-century `float` to `double` please do so. That will need `strtod()`. – Weather Vane Dec 23 '19 at 19:07
  • 1
    Use `printf("%.*f", places, value);` or `printf("%.*f", places1 < places2 ? places1 : places2, value);` – Weather Vane Dec 23 '19 at 19:09
  • Perhaps some kind soul will post free code for you, but that's not what SO is about. Have a go at implementing what's in my first comment to find the decimal places in each input. – Weather Vane Dec 23 '19 at 19:20
  • I don't understand what you are trying to achieve here. You seem to want to output the result with the minimum of the number of decimal places of the two operands -- is that right? But for multiplication, this makes no sense mathematically. You get different results for `12.3 * 4.567` vs `1.23 * 45.67`, which is surely not what you want? For multiplication and division (as opposed to addition and subtraction), you want to retain significant figures, not decimal places. – TonyK Dec 23 '19 at 23:02
  • 80.55 * 879.5689 = ‭70849.274895‬, not 70849.281250. Why do you want a rounded output of 70849.28 instead of 70849.27? – chux - Reinstate Monica Dec 25 '19 at 01:14
  • @chux-ReinstateMonica That's the value I got from the code - I didn't check the output with a proper calculator, my mistake. – stringtheorv Dec 25 '19 at 17:01
  • @TonyK I'm referring to the number of digits after the decimal points. So I'd want the first to be reduced to 56.2 (since 12.3 has a single digit post-point) and the second to 56.17 from 56.1741. As for why, I'd taken a series of tests where the final answer has to be given in these terms or point are docked - I'm just starting out with C, was working through basic examples of which multiplication of floats was one, and thought trying to reproduce that would be a learning experience of some sort. – stringtheorv Dec 25 '19 at 17:08
  • @WeatherVane At no point in the question have I even hinted at wanting free code, and I've got to say, this is the first time I've encountered someone that is condescending over data types - for your information, every single beginner module (20th century or otherwise) that I've come across uses float since there is no imaginable use for over 6 decimal places if you're learning operator syntax. – stringtheorv Dec 25 '19 at 17:45
  • @WeatherVane I understand why you'd make certain assumptions re: website novelty but kindly remember that 'making a separate or new account' is a thing that's possible. Regardless, thank you for pointing me toward relevant functions - I'll attempt to implement them with my ostensibly pea-sized brain. Happy Christmas. – stringtheorv Dec 25 '19 at 17:45
  • @stringtheorv and a merry Christmas to you too. I have no idea what your last remark was about, as I made no reference to accounts, websites, or your intelligence, which I am sure is great. My comment about free code was because you haven't posted any code showing your problem, and understanding, as is expected here. My remark about `double` vs `float` was a suggestion on the grounds that these days the inferior `float` should only be used for a very good reason, such as memory restrictions, performance or ability of embedded code, etc, and there is a lot of obsolete teaching material around. – Weather Vane Dec 25 '19 at 18:53
  • ...and if you come here with a "how do I?" question without posting anything else, it's not very easy to know where to pitch a response. – Weather Vane Dec 25 '19 at 18:59
  • @stringtheorv When you say `So if 80.55`. are you referring to 1) the user input text of "80.55" 2) code such as `float x = 80.55;` 3) the nearest `float` to 80.55 or 4) the nearest `double` to 80.55. Each of these has subtle different meaning. Recall that `80.55` is not exactly representable as a floating point value for the same reason 1/3 is not exactly representable in decimal. – chux - Reinstate Monica Dec 26 '19 at 05:46

1 Answers1

0

How to get the number of decimal places of a float value?
Is there a way to get the number of decimal places of two entered floats?

  1. Read a line of user input to a string. Strongly recommend fgets().

  2. Search for the decimal point with strchr() or helper code and the end of the number with "%n".

Perhaps:

static int find_prec(const char *s, int length) {
  for (int i=0; i < length; i++) {
    if (s[i] == '.') {
      i++;
      int p = 0;
      while (isdigit((unsigned char) s[i+p])) {
        p++;
      }
      return p;
    }
  }  
  return 0;
}

...
if (fgets(buffer, sizeof buffer, stdin)) {
  double x[2];
  int n[2];
  if (sscanf(buffer, "%f%n %f%n", &x[0], &n[0], &x[1], &n[1]) == 2) {
    int num_dp[2];
    num_dp[0] = find_prec(buffer, n[0]);
    num_dp[1] = find_prec(buffer + n[0], n[1] - n[0]);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256