Line data Source code
1 : /* Copyright 1989-2024 Free Software Foundation, Inc. 2 : Written by James Clark (jjc@jclark.com) 3 : 4 : This file is part of groff, the GNU roff typesetting system. 5 : 6 : groff is free software; you can redistribute it and/or modify it under 7 : the terms of the GNU General Public License as published by the Free 8 : Software Foundation, either version 3 of the License, or 9 : (at your option) any later version. 10 : 11 : groff is distributed in the hope that it will be useful, but WITHOUT ANY 12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 : for more details. 15 : 16 : You should have received a copy of the GNU General Public License 17 : along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 : 19 : #ifdef HAVE_CONFIG_H 20 : #include <config.h> 21 : #endif 22 : 23 : #include "lib.h" 24 : 25 : #ifdef __cplusplus 26 : extern "C" { 27 : #endif 28 : 29 856826 : const char *if_to_a(int i, int decimal_point) 30 : { 31 : static char buf[INT_DIGITS + 3]; // INT_DIGITS + '-', '.', '\0' 32 856826 : char *p = buf + INT_DIGITS + 2; 33 856826 : int point = 0; 34 856826 : buf[INT_DIGITS + 2] = '\0'; 35 : /* assert(decimal_point <= INT_DIGITS); */ 36 856826 : if (i >= 0) { 37 : do { 38 4206591 : *--p = '0' + (i % 10); 39 4206591 : i /= 10; 40 4206591 : if (++point == decimal_point) 41 809391 : *--p = '.'; 42 4206591 : } while (i != 0 || point < decimal_point); 43 : } 44 : else { /* i < 0 */ 45 : do { 46 213250 : *--p = '0' - (i % 10); 47 213250 : i /= 10; 48 213250 : if (++point == decimal_point) 49 47435 : *--p = '.'; 50 213250 : } while (i != 0 || point < decimal_point); 51 47435 : *--p = '-'; 52 : } 53 856826 : if (decimal_point > 0) { 54 : char *q; 55 : /* there must be a dot, so this will terminate */ 56 1680520 : for (q = buf + INT_DIGITS + 2; q[-1] == '0'; --q) 57 : ; 58 856826 : if (q[-1] == '.') { 59 143549 : if (q - 1 == p) { 60 98182 : q[-1] = '0'; 61 98182 : q[0] = '\0'; 62 : } 63 : else 64 45367 : q[-1] = '\0'; 65 : } 66 : else 67 713277 : *q = '\0'; 68 : } 69 856826 : return p; 70 : } 71 : 72 : #ifdef __cplusplus 73 : } 74 : #endif 75 : 76 : // Local Variables: 77 : // fill-column: 72 78 : // mode: C++ 79 : // End: 80 : // vim: set cindent noexpandtab shiftwidth=2 textwidth=72: