SHARE
TWEET

string_to_real()

a guest Apr 18th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @param string
  2. /// @jujuadams 2019/04/18
  3.  
  4. var _string = argument0;
  5.  
  6. var _fail_value = 0;
  7. var _hit_space  = false;
  8. var _hit_number = false;
  9.  
  10. var _size = string_length(_string);
  11. for(var _i = _size; _i > 0; _i--)
  12. {
  13.     var _character = string_char_at(_string, _i);
  14.     switch(_character)
  15.     {
  16.         case "0":
  17.         case "1":
  18.         case "2":
  19.         case "3":
  20.         case "4":
  21.         case "5":
  22.         case "6":
  23.         case "7":
  24.         case "8":
  25.         case "9":
  26.         case ".":
  27.         case "-":
  28.             if (_hit_space && _hit_number) return _fail_value;
  29.             _hit_space  = false;
  30.             _hit_number = true;
  31.         break;
  32.        
  33.         case " ":
  34.             _string = string_delete(_string, _i, 1);
  35.             _hit_space = true;
  36.         break;
  37.        
  38.         default:
  39.             return _fail_value;
  40.         break;
  41.     }
  42. }
  43.  
  44. if (string_count("-", _string) > 1) return undefined;
  45. if (string_count(".", _string) > 1) return undefined;
  46.  
  47. var _negative_pos = string_pos("-", _string);
  48. if (_negative_pos > 1) return undefined;
  49. if (string_pos(".", _string) == (1+_negative_pos)) return undefined;
  50.  
  51. return real(_string);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top