lc_parse_number (public)

 lc_parse_number num locale [ integer_only_p ]

Defined in packages/acs-lang/tcl/localization-procs.tcl

Converts a number to its canonical representation by stripping everything but the decimal separator and trimming left 0's so it won't be octal. It can process the following types of numbers:

  • Just digits (allows leading zeros).
  • Digits with a valid thousands separator, used consistently (leading zeros not allowed)
  • Either of the above with a decimal separator plus optional digits after the decimal marker
The valid separators are taken from the given locale. Does not handle localized signed numbers in this version. The sign may only be placed before the number (with/without whitespace). Also allows the empty string, returning same.

Parameters:
num (required)
Localized number
locale (required)
Locale
integer_only_p (optional, defaults to "0")
True if only integers returned
Returns:
Canonical form of the number
Error:
If unsupported locale or not a number

Partial Call Graph (max 5 caller/called nodes):
%3 test_lang_test__lc_procs lang_test__lc_procs (test acs-lang) lc_parse_number lc_parse_number test_lang_test__lc_procs->lc_parse_number lang::util::escape_vars_if_not_null lang::util::escape_vars_if_not_null (private) lc_parse_number->lang::util::escape_vars_if_not_null lc_get lc_get (public) lc_parse_number->lc_get util::trim_leading_zeros util::trim_leading_zeros (public) lc_parse_number->util::trim_leading_zeros Class ::xowiki::formfield::numeric Class ::xowiki::formfield::numeric (public) Class ::xowiki::formfield::numeric->lc_parse_number

Testcases:
lang_test__lc_procs
Source code:
    if {$num eq ""} {
        return ""
    }

    set dec  [lc_get -locale $locale "decimal_point"]
    set thou [lc_get -locale $locale "mon_thousands_sep"][lc_get -locale $locale "thousands_sep"]
    set neg  [lc_get -locale $locale "negative_sign"]
    set pos  [lc_get -locale $locale "positive_sign"]

    #
    # Sanity check: decimal point must be different from the thousands
    # separators. This test should be really either in regression
    # testing or be forumulated as constraint after changing the
    # message keys.  However, since a violation can lead to incorrect
    # results, the safety check is here as well.
    #
    if {[string first $dec $thou] > -1} {
        error "error in locale $locale: decimal point '$decimal_point' must be different from thousands separator (mon_thousands_sep '[lc_get -locale $locale mon_thousands_sep]' and thousands_sep '[lc_get -locale $locale thousands_sep]')"
    }

    lang::util::escape_vars_if_not_null {dec thou neg pos}

    # Pattern actually looks like this (separators notwithstanding):
    # {^\ *([-]|[+])?\ *([0-9]+|[1-9][0-9]{1,2}([,][0-9]{3})+)([.][0-9]*)?\ *$}

    set pattern "^\\ *($neg|$pos)?\\ *((\[0-9\]+|\[1-9\]\[0-9\]{0,2}($thou\[0-9\]\{3\})+)"

    if {$integer_only_p} {
        append pattern "?)(${dec}0*)?"
    } else {
        append pattern "?($dec\[0-9\]*)?)"
    }

    append pattern "\\ *\$"

    set is_valid_number  [regexp -- $pattern $num match sign number]

    if {!$is_valid_number} {
        error "Not a number $num"
    } else {

        regsub -all $thou $number "" number

        if {!$integer_only_p} {
            regsub -all $dec $number "." number
        }

        set number [util::trim_leading_zeros $number]

        # Last pathological case
        if {"." eq $number } {
            set number 0
        }

        if {[string match "\\\\\\${sign}" $neg]} {
            set number -$number
        }

        return $number
    }
XQL Not present:
PostgreSQL, Oracle
Generic XQL file:
packages/acs-lang/tcl/localization-procs.xql

[ hide source ] | [ make this the default ]
Show another procedure: