Convert VAL to a human format string using PRECISION in BUF of size BUF_SIZE. Use SCALE, GROUP, and ROUND to format. Return the number of bytes needed to represent VAL. If this number is not less than BUF_SIZE, the buffer is too small; if it is negative, the formatting failed for some reason. */
| 777 | less than BUF_SIZE, the buffer is too small; if it is negative, the |
| 778 | formatting failed for some reason. */ |
| 779 | static int |
| 780 | double_to_human (long double val, int precision, |
| 781 | char *buf, idx_t buf_size, |
| 782 | enum scale_type scale, int group, enum round_type round) |
| 783 | { |
| 784 | char fmt[sizeof "%'0.*Lfi%s%s%s%s" + INT_STRLEN_BOUND (zero_padding_width)]; |
| 785 | char *pfmt = fmt; |
| 786 | *pfmt++ = '%'; |
| 787 | |
| 788 | if (group) |
| 789 | *pfmt++ = '\''; |
| 790 | |
| 791 | if (zero_padding_width) |
| 792 | pfmt += sprintf (pfmt, "0%d", zero_padding_width); |
| 793 | |
| 794 | devmsg ("double_to_human:\n"); |
| 795 | |
| 796 | if (scale == scale_none) |
| 797 | { |
| 798 | val *= powerld (10, precision); |
| 799 | val = simple_round (val, round); |
| 800 | val /= powerld (10, precision); |
| 801 | |
| 802 | devmsg ((group) ? |
| 803 | " no scaling, returning (grouped) value: %'.*Lf\n" : |
| 804 | " no scaling, returning value: %.*Lf\n", precision, val); |
| 805 | |
| 806 | strcpy (pfmt, ".*Lf%s"); |
| 807 | |
| 808 | return snprintf (buf, buf_size, fmt, precision, val, |
| 809 | suffix ? suffix : ""); |
| 810 | } |
| 811 | |
| 812 | /* Scaling requested by user. */ |
| 813 | double scale_base = default_scale_base (scale); |
| 814 | |
| 815 | /* Normalize val to scale. */ |
| 816 | int power = 0; |
| 817 | val = expld (val, scale_base, &power); |
| 818 | devmsg (" scaled value to %Lf * %0.f ^ %d\n", val, scale_base, power); |
| 819 | |
| 820 | /* Perform rounding. */ |
| 821 | int power_adjust = 0; |
| 822 | if (user_precision != -1) |
| 823 | power_adjust = MIN (power * 3, user_precision); |
| 824 | else if (absld (val) < 10) |
| 825 | { |
| 826 | /* for values less than 10, we allow one decimal-point digit, |
| 827 | so adjust before rounding. */ |
| 828 | power_adjust = 1; |
| 829 | } |
| 830 | |
| 831 | val *= powerld (10, power_adjust); |
| 832 | val = simple_round (val, round); |
| 833 | val /= powerld (10, power_adjust); |
| 834 | |
| 835 | /* two special cases after rounding: |
| 836 | 1. a "999.99" can turn into 1000 - so scale down |
no test coverage detected