From 786c654db53dcd08790844b2a599a255f79c4422 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Sat, 8 Apr 2006 09:48:13 +0000 Subject: [PATCH] ICU-5152 Remove broken uprv_dtostr function, and create a better ChoiceFormat::dtos function. X-SVN-Rev: 19525 --- icu4c/source/common/ustrfmt.c | 31 +------------------------------ icu4c/source/common/ustrfmt.h | 4 +--- icu4c/source/i18n/choicfmt.cpp | 33 +++++++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/icu4c/source/common/ustrfmt.c b/icu4c/source/common/ustrfmt.c index 78597692e1d..5e9fb924f8d 100644 --- a/icu4c/source/common/ustrfmt.c +++ b/icu4c/source/common/ustrfmt.c @@ -1,42 +1,13 @@ /* ********************************************************************** -* Copyright (C) 2001-2005, International Business Machines +* Copyright (C) 2001-2006, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** */ #include "cstring.h" #include "ustrfmt.h" -#include -U_CAPI char* U_EXPORT2 -uprv_dtostr(double value, char *buffer, int32_t maximumDigits,UBool fixedPoint) -{ - char *itrPtr = buffer + 1; /* skip '-' or a number before the decimal */ - char *startPtr; - - sprintf(buffer,"%f",value); - - /* Find the decimal point. - Some unusal machines use a comma when the system locale changes - */ - while (isalnum(*itrPtr)) { - itrPtr++; - } - *itrPtr = '.'; - - /* truncate trailing zeros, except the one after '.' */ - startPtr = itrPtr + 1; - itrPtr = uprv_strchr(startPtr, 0); - while(--itrPtr > startPtr){ - if(*itrPtr == '0'){ - *itrPtr = 0; - }else{ - break; - } - } - return buffer; -} /*** * Fills in a UChar* string with the radix-based representation of a diff --git a/icu4c/source/common/ustrfmt.h b/icu4c/source/common/ustrfmt.h index a5518516b4e..f1891bdabf5 100644 --- a/icu4c/source/common/ustrfmt.h +++ b/icu4c/source/common/ustrfmt.h @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 2001-2005, International Business Machines +* Copyright (C) 2001-2006, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** */ @@ -10,8 +10,6 @@ #include "unicode/utypes.h" -U_CAPI char* U_EXPORT2 -uprv_dtostr(double value, char *buffer, int32_t maximumDigits,UBool fixedPoint); U_CAPI int32_t U_EXPORT2 uprv_itou (UChar * buffer, int32_t capacity, uint32_t i, uint32_t radix, int32_t minwidth); diff --git a/icu4c/source/i18n/choicfmt.cpp b/icu4c/source/i18n/choicfmt.cpp index eeac7f381ca..b9caa9a3b9c 100644 --- a/icu4c/source/i18n/choicfmt.cpp +++ b/icu4c/source/i18n/choicfmt.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 1997-2004, International Business Machines Corporation and * +* Copyright (C) 1997-2006, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* * @@ -31,9 +31,10 @@ #include "unicode/numfmt.h" #include "unicode/locid.h" #include "cpputils.h" -#include "ustrfmt.h" #include "cstring.h" #include "putilimp.h" +#include +#include // ***************************************************************************** // class ChoiceFormat @@ -210,9 +211,33 @@ UnicodeString& ChoiceFormat::dtos(double value, UnicodeString& string) { - char temp[256]; + /* Buffer to contain the digits and any extra formatting stuff. */ + char temp[DBL_DIG + 16]; + char *itrPtr = temp; + char *startPtr; - uprv_dtostr(value, temp, 3, TRUE); + sprintf(temp, "%.*f", DBL_DIG, value); + + /* Find and convert the decimal point. + Using setlocale on some machines will cause sprintf to use a comma for certain locales. + */ + while (*itrPtr && (*itrPtr == '-' || isdigit(*itrPtr))) { + itrPtr++; + } + if (*itrPtr) { + *itrPtr = '.'; + } + + /* remove trailing zeros, except the one after '.' */ + startPtr = itrPtr + 1; + itrPtr = uprv_strchr(startPtr, 0); + while(--itrPtr > startPtr){ + if(*itrPtr == '0'){ + *itrPtr = 0; + }else{ + break; + } + } string = UnicodeString(temp, -1, US_INV); /* invariant codepage */ return string; }