DoubleToString.h (1926B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef util_DoubleToString_h 8 #define util_DoubleToString_h 9 10 /* 11 * Public interface to portable double-precision floating point to string 12 * and back conversion package. 13 */ 14 15 struct DtoaState; 16 17 namespace js { 18 19 extern DtoaState* NewDtoaState(); 20 21 extern void DestroyDtoaState(DtoaState* state); 22 23 } // namespace js 24 25 /* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD 26 * or DTOSTR_STANDARD_EXPONENTIAL conversion can produce. This maximum is 27 * reached for a number like -0.0000012345678901234567. */ 28 #define DTOSTR_STANDARD_BUFFER_SIZE 26 29 30 /* 31 * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a 32 * better function to use. 33 * 34 * Convert d to a string in the given base. The integral part of d will be 35 * printed exactly in that base, regardless of how large it is, because there 36 * is no exponential notation for non-base-ten numbers. The fractional part 37 * will be rounded to as few digits as possible while still preserving the 38 * round-trip property (analogous to that of printing decimal numbers). In 39 * other words, if one were to read the resulting string in via a hypothetical 40 * base-number-reading routine that rounds to the nearest IEEE double (and to 41 * an even significand if there are two equally near doubles), then the result 42 * would equal d (except for -0.0, which converts to "0", and NaN, which is 43 * not equal to itself). 44 * 45 * Return nullptr if out of memory. If the result is not nullptr, it must be 46 * released via js_free(). 47 */ 48 char* js_dtobasestr(DtoaState* state, int base, double d); 49 50 #endif /* util_DoubleToString_h */