Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const char * const prefix = "yzafpnum KMGTPEZY";
- const float prefix_xf[] = {
- 1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3,
- 1e+1,
- 1e+3, 1e+6, 1e+9, 1e+12, 1e+15, 1e+18, 1e+21, 1e+24};
- float hum_to_float(const char *s) {
- // This function stolen from either Rolf Neugebauer or Andrew Tolmach.
- // Probably Rolf.
- float a = 0.0f;
- int e = 0;
- int c;
- float sign = 1;
- c = *s++;
- if(c == '-') {
- sign = -1;
- c = *s++;
- }
- while(c != '\0' && isdigit(c)) {
- a = a * 10.0f + (c - '0');
- c = *s++;
- }
- if(c == '.') {
- while((c = *s++) != '\0' && isdigit(c)) {
- a = a * 10.0f + (c - '0');
- e = e - 1;
- }
- }
- if(c <= 'z' && c >= 'E') {
- for(int i = 0; prefix[i] != '\0'; i++) {
- if(c == prefix[i]){
- a *= prefix_xf[i];
- break;
- }
- }
- }
- while(e > 0) {
- a *= 10.0f;
- e--;
- }
- while(e < 0) {
- a *= 0.1f;
- e++;
- }
- return a * sign;
- }
Add Comment
Please, Sign In to add comment