C convert section of char array to double

安稳与你 提交于 2019-12-12 05:49:11

问题


I want to convert a section of a char array to a double. For example I have:

char in_string[] = "4014.84954";

Say I want to convert the first 40 to a double with value 40.0. My code so far:

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

int main(int arg) {
  char in_string[] = "4014.84954";
  int i = 0;
  for(i = 0; i <= sizeof(in_string); i++) {
    printf("%c\n", in_string[i]);
    printf("%f\n", atof(&in_string[i]));
  }
}

In each loop atof it converts the char array from the starting pointer I supply all the way to the end of the array. The output is:

4
4014.849540
0
14.849540
1
14.849540
4
4.849540
.
0.849540
8
84954.000000   etc...

How can I convert just a portion of a char array to a double? This must by modular because my real input_string is much more complicated, but I will ensure that the char is a number 0-9.


回答1:


The following should work assuming:

I will ensure that the char is a number 0-9.

double toDouble(const char* s, int start, int stop) {
    unsigned long long int m = 1;
    double ret = 0;
    for (int i = stop; i >= start; i--) {
        ret += (s[i] - '0') * m;
        m *= 10;
    }
    return ret;
}

For example for the string 23487 the function will do this calculations:

ret = 0
ret += 7 * 1
ret += 8 * 10
ret += 4 * 100
ret += 3 * 1000
ret += 2 * 10000
ret = 23487



回答2:


You can copy the desired amount of the string you want to another char array, null terminate it, and then convert it to a double. EG, if you want 2 digits, copy the 2 digits you want into a char array of length 3, ensuring the 3rd character is the null terminator.

Or if you don't want to make another char array, you can back up the (n+1)th char of the char array, replace it with a null terminator (ie 0x00), call atof, and then replace the null terminator with the backed up value. This will make atof stop parsing where you placed your null terminator.




回答3:


Just use sscanf. Use the format "ld" and check for return value is one.




回答4:


What about that, insert NULL at the right position and then revert it back to the original letter? This means you will manipulate the char array but you will revert it back to the original at the end.




回答5:


You can create a function that will make the work in a temporary string (on the stack) and return the resulting double:

double atofn (char *src, int n) {
  char tmp[50]; // big enough to fit any double

  strncpy (tmp, src, n);
  tmp[n] = 0;
  return atof(tmp);
}



回答6:


How much simpler could it get than sscanf?

#include <assert.h>
#include <stdio.h>

int main(void) {
    double foo;
    assert(sscanf("4014.84954", "%02lf", &foo) == 1);
    printf("Processed the first two bytes of input and got: %lf\n", foo);

    assert(sscanf("4014.84954" + 2, "%05lf", &foo) == 1);
    printf("Processed the next five bytes of input and got: %lf\n", foo);

    assert(sscanf("4014.84954" + 7, "%lf", &foo) == 1);
    printf("Processed the rest of the input and got: %lf\n", foo);
    return 0;
}


来源:https://stackoverflow.com/questions/15583965/c-convert-section-of-char-array-to-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!