mysql_float_to_double.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Keyur Govande <kgovande@gmail.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifndef MYSQL_FLOAT_TO_DOUBLE_H
  19. #define MYSQL_FLOAT_TO_DOUBLE_H
  20. #include "main/php.h"
  21. #include <float.h>
  22. #include "main/snprintf.h"
  23. #define MAX_CHAR_BUF_LEN 255
  24. #ifndef FLT_DIG
  25. # define FLT_DIG 6
  26. #endif
  27. /*
  28. * Convert from a 4-byte float to a 8-byte decimal by first converting
  29. * the float to a string, and then the string to a double.
  30. * The decimals argument specifies the precision of the output. If decimals
  31. * is less than zero, then a gcvt(3) like logic is used with the significant
  32. * digits set to FLT_DIG i.e. 6.
  33. */
  34. static inline double mysql_float_to_double(float fp4, int decimals) {
  35. char num_buf[MAX_CHAR_BUF_LEN]; /* Over allocated */
  36. if (decimals < 0) {
  37. php_gcvt(fp4, FLT_DIG, '.', 'e', num_buf);
  38. } else {
  39. php_sprintf(num_buf, "%.*f", decimals, fp4);
  40. }
  41. return zend_strtod(num_buf, NULL);
  42. }
  43. /*
  44. * Local variables:
  45. * tab-width: 4
  46. * c-basic-offset: 4
  47. * End:
  48. * vim600: noet sw=4 ts=4 fdm=marker
  49. * vim<600: noet sw=4 ts=4
  50. */
  51. #endif /* MYSQL_FLOAT_TO_DOUBLE_H */