This feature was removed by
4aead68cdb86ca60cc372f0cd558cadda90ddec5.
* module/ice-9/i18n.scm (number-decimal-string): Rewrite the case where
DIGIT-COUNT is not an integer.
(number->locale-string): Always pass FRACTION-DIGITS to
'number-decimal-string'.
* test-suite/tests/format.test ("~h localized number")["1234.5"]
["padding", "padchar"]: Remove decimal specifier.
* test-suite/tests/i18n.test ("number->locale-string")
["fraction",
* test-suite/tests/i18n.test ("format ~h")["12 345,678"]: Remove decimal
specifier. Remove one decimal.
* doc/ref/api-i18n.texi (Number Input and Output): Update
'number->locale-string' doc to mention the number of decimals.
@c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual.
-@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2009, 2010
-@c Free Software Foundation, Inc.
+@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007,
+@c 2009, 2010, 2017 Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions.
@node Internationalization
@deffn {Scheme Procedure} number->locale-string number [fraction-digits [locale]]
Convert @var{number} (an inexact) into a string according to the
cultural conventions of either @var{locale} (a locale object) or the
-current locale. Optionally, @var{fraction-digits} may be bound to an
-integer specifying the number of fractional digits to be displayed.
+current locale. By default, print as many fractional digits as
+necessary, up to an upper bound. Optionally, @var{fraction-digits} may
+be bound to an integer specifying the number of fractional digits to be
+displayed.
@end deffn
@deffn {Scheme Procedure} monetary-amount->locale-string amount intl? [locale]
digits))))))
(define (number-decimal-string number digit-count)
- "Return a string representing the decimal part of NUMBER, with exactly
-DIGIT-COUNT digits"
- (if (integer? number)
- (make-string digit-count #\0)
-
- ;; XXX: This is brute-force and could be improved by following one
- ;; of the "Printing Floating-Point Numbers Quickly and Accurately"
- ;; papers.
+ "Return a string representing the decimal part of NUMBER. When
+DIGIT-COUNT is an integer, return exactly DIGIT-COUNT digits; when
+DIGIT-COUNT is #t, return as many decimals as necessary, up to an
+arbitrary limit."
+ (define max-decimals
+ 5)
+
+ ;; XXX: This is brute-force and could be improved by following one of
+ ;; the "Printing Floating-Point Numbers Quickly and Accurately"
+ ;; papers.
+ (if (integer? digit-count)
(let ((number (* (expt 10 digit-count)
(- number (floor number)))))
(string-pad (integer->string (round (inexact->exact number)))
digit-count
- #\0))))
+ #\0))
+ (let loop ((decimals 0))
+ (let ((number' (* number (expt 10 decimals))))
+ (if (or (= number' (floor number'))
+ (>= decimals max-decimals))
+ (let* ((fraction (- number'
+ (* (floor number)
+ (expt 10 decimals))))
+ (str (integer->string
+ (round (inexact->exact fraction)))))
+ (if (zero? fraction)
+ ""
+ str))
+ (loop (+ decimals 1)))))))
(define (%number-integer-part int grouping separator)
;; Process INT (a string denoting a number's integer part) and return a new
(locale %global-locale))
"Convert @var{number} (an inexact) into a string according to the cultural
conventions of either @var{locale} (a locale object) or the current locale.
+By default, print as many fractional digits as necessary, up to an upper bound.
Optionally, @var{fraction-digits} may be bound to an integer specifying the
number of fractional digits to be displayed."
(floor (abs number)))))
(dec (decimal-part
(number-decimal-string (abs number)
- (if (integer?
- fraction-digits)
- fraction-digits
- 0))))
+ fraction-digits)))
(grouping (locale-digit-grouping locale))
(separator (locale-thousands-separator locale)))
(with-test-prefix "~h localized number"
(pass-if "1234.5"
- (string=? (format #f "~,1h" 1234.5) "1234.5"))
+ (string=? (format #f "~h" 1234.5) "1234.5"))
(pass-if "padding"
- (string=? (format #f "~6h" 123.2) " 123"))
+ (string=? (format #f "~6h" 123.2) " 123.2"))
(pass-if "padchar"
- (string=? (format #f "~8,1,'*h" 123.2) "***123.2"))
+ (string=? (format #f "~8,,'*h" 123.2) "***123.2"))
(pass-if "decimals"
(string=? (format #f "~,2h" 123.4567)
(pass-if-equal "fraction"
"1234.567"
- (number->locale-string 1234.567 3))
+ (number->locale-string 1234.567))
(pass-if-equal "fraction, 1 digit"
"1234.6"
(under-french-locale-or-unresolved
(lambda ()
(let ((fr (make-locale LC_ALL %french-locale-name)))
- (number->locale-string 1234.567 3 fr)))))
+ (number->locale-string 1234.567 #t fr)))))
(pass-if-equal "fraction, 1 digit"
"1 234,6"
(with-test-prefix "French"
- (pass-if-equal "12345.6789"
- "12 345,6789"
+ (pass-if-equal "12345.678"
+ "12 345,678"
(under-french-locale-or-unresolved
(lambda ()
(if (null? (locale-digit-grouping %french-locale))
(throw 'unresolved)
- (format #f "~,4:h" 12345.6789 %french-locale))))))
+ (format #f "~:h" 12345.678 %french-locale))))))
(with-test-prefix "English"
- (pass-if-equal "12345.6789"
- "12,345.6789"
+ (pass-if-equal "12345.678"
+ "12,345.678"
(under-american-english-locale-or-unresolved
(lambda ()
(if (null? (locale-digit-grouping %american-english-locale))
(throw 'unresolved)
- (format #f "~,4:h" 12345.6789
+ (format #f "~:h" 12345.678
%american-english-locale)))))))
(with-test-prefix "monetary-amount->locale-string"