def _group(conv, s, monetary=False): thousands_sep = conv[monetary and 'mon_thousands_sep' or 'thousands_sep'] grouping = conv[monetary and 'mon_grouping' or 'grouping'] if not grouping: return (s, 0) if s[-1] == ' ': stripped = s.rstrip() right_spaces = s[len(stripped):] s = stripped else: right_spaces = '' left_spaces = '' groups = [] for interval in locale._grouping_intervals(grouping): if not s or s[-1] not in "0123456789": # only non-digit characters remain (sign, spaces) left_spaces = s s = '' break groups.append(s[-interval:]) s = s[:-interval] if s: groups.append(s) groups.reverse() return ( left_spaces + thousands_sep.join(groups) + right_spaces, len(thousands_sep) * (len(groups) - 1) )
def currency(val, localeconv=None, international=False): """Formats val according to the currency settings for current language.""" val = Decimal(val) conv = localeconv_cache.getconv() conv.update(localeconv or settings.LOCALECONV) # split integer part and fraction parts = str(abs(val)).split('.') # grouping groups = [] s = parts[0] for interval in locale._grouping_intervals(conv['mon_grouping']): if not s: break groups.append(s[-interval:]) s = s[:-interval] if s: groups.append(s) groups.reverse() s = smart_text(conv['mon_thousands_sep']).join(groups) # display fraction for non integer values if len(parts) > 1: s += smart_text(conv['mon_decimal_point']) + parts[1] # '<' and '>' are markers if the sign must be inserted between symbol and value s = '<' + s + '>' smb = smart_text(conv[international and 'int_curr_symbol' or 'currency_symbol']) precedes = conv[val < 0 and 'n_cs_precedes' or 'p_cs_precedes'] separated = conv[val < 0 and 'n_sep_by_space' or 'p_sep_by_space'] if precedes: s = smb + (separated and ' ' or '') + s else: s = s + (separated and ' ' or '') + smb sign_pos = conv[val < 0 and 'n_sign_posn' or 'p_sign_posn'] sign = conv[val < 0 and 'negative_sign' or 'positive_sign'] if sign_pos == 0: s = '(' + s + ')' elif sign_pos == 1: s = sign + s elif sign_pos == 2: s = s + sign elif sign_pos == 3: s = s.replace('<', sign) elif sign_pos == 4: s = s.replace('>', sign) else: # the default if nothing specified; # this should be the most fitting sign position s = sign + s return s.replace('<', '').replace('>', '')
def currency(val, localeconv=None, international=False): """Formats val according to the currency settings for current language.""" val = Decimal(val) conv = localeconv_cache.getconv() conv.update(localeconv or settings.LOCALECONV) # split integer part and fraction parts = str(abs(val)).split(".") # grouping groups = [] s = parts[0] for interval in locale._grouping_intervals(conv["mon_grouping"]): if not s: break groups.append(s[-interval:]) s = s[:-interval] if s: groups.append(s) groups.reverse() s = smart_text(conv["mon_thousands_sep"]).join(groups) # display fraction for non integer values if len(parts) > 1: s += smart_text(conv["mon_decimal_point"]) + parts[1] # '<' and '>' are markers if the sign must be inserted between symbol and value s = "<" + s + ">" smb = smart_text(conv[international and "int_curr_symbol" or "currency_symbol"]) precedes = conv[val < 0 and "n_cs_precedes" or "p_cs_precedes"] separated = conv[val < 0 and "n_sep_by_space" or "p_sep_by_space"] if precedes: s = smb + (separated and " " or "") + s else: s = s + (separated and " " or "") + smb sign_pos = conv[val < 0 and "n_sign_posn" or "p_sign_posn"] sign = conv[val < 0 and "negative_sign" or "positive_sign"] if sign_pos == 0: s = "(" + s + ")" elif sign_pos == 1: s = sign + s elif sign_pos == 2: s = s + sign elif sign_pos == 3: s = s.replace("<", sign) elif sign_pos == 4: s = s.replace(">", sign) else: # the default if nothing specified; # this should be the most fitting sign position s = sign + s return s.replace("<", "").replace(">", "")
def currency(val, international=False): """Formats val according to the currency settings for current language.""" digits = settings.PRICE_DECIMAL_PLACES # grouping groups = [] s = str(abs(int(val))) for interval in locale._grouping_intervals(localeconv['mon_grouping']): if not s: break groups.append(s[-interval:]) s = s[:-interval] if s: groups.append(s) groups.reverse() s = smart_text(localeconv['mon_thousands_sep']).join(groups) # display fraction for non integer values if digits and not isinstance(val, int): s += smart_text(localeconv['mon_decimal_point']) + '{{:.{}f}}'.format(digits).format(val).split('.')[1] # '<' and '>' are markers if the sign must be inserted between symbol and value s = '<' + s + '>' smb = smart_text(localeconv[international and 'int_curr_symbol' or 'currency_symbol']) precedes = localeconv[val < 0 and 'n_cs_precedes' or 'p_cs_precedes'] separated = localeconv[val < 0 and 'n_sep_by_space' or 'p_sep_by_space'] if precedes: s = smb + (separated and ' ' or '') + s else: s = s + (separated and ' ' or '') + smb sign_pos = localeconv[val < 0 and 'n_sign_posn' or 'p_sign_posn'] sign = localeconv[val < 0 and 'negative_sign' or 'positive_sign'] if sign_pos == 0: s = '(' + s + ')' elif sign_pos == 1: s = sign + s elif sign_pos == 2: s = s + sign elif sign_pos == 3: s = s.replace('<', sign) elif sign_pos == 4: s = s.replace('>', sign) else: # the default if nothing specified; # this should be the most fitting sign position s = sign + s return s.replace('<', '').replace('>', '').replace(' ', '\u00A0')
def update_event(self, inp=-1): self.set_output_val(0, locale._grouping_intervals(self.input(0)))