Bugzilla – Attachment 85942 Details for
Bug 51008
[PT] FORMATTING: Thousands separator should be Non-Breaking Space
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
Python script with all relevant functions to solve this bug implemented.
thousep.py (text/x-python), 4.31 KB, created by
severoraz
on 2013-09-17 01:00:19 UTC
(
hide
)
Description:
Python script with all relevant functions to solve this bug implemented.
Filename:
MIME Type:
Creator:
severoraz
Created:
2013-09-17 01:00:19 UTC
Size:
4.31 KB
patch
obsolete
>#/usr/bin/env python > ># Wolter Hellmund <wolterh6@gmail.com> ># CC-BY-SA > ># This is a sample file that implements: ># a) a number format interpreter, similar to that which currently exists ># in libreoffice; ># b) a number string significant figure formatter; ># c) a number string thousand separator formatter; ># d) a short demo. > ># Takes a string that represents a desired number format and interprets ># it. The string must have the format #+{,#+}? >def interpret_format(formatstr): > sigfigs = formatstr.count('#') > wholefigs = formatstr.find(',') > return sigfigs, wholefigs > ># Takes strings representing a number and returns a string representing ># the number with the desired sigfig configuration and an exponent. ># Parameters: ># whole: string representing the whole part of the number ># decimal: string representing the decimal part of the number (may have ># leading zeroes) ># sigfigs: amount of desired significant figures ># wholefigs: amount of desired whole significant figures. (default: no ># specification.) >def sigfigs_str(whole, decimal, sigfigs, wholefigs=0): > # Unaltered scientific notation > exp = 0 > exp -= len(decimal) > newnumber = (whole+decimal) > exp += len(newnumber[sigfigs:]) > # Compensate for short numbers > if sigfigs > len(newnumber): > sigfigs = len(newnumber)-1 > # Round > if int(newnumber[sigfigs]) >= 5: > newnumber = newnumber[:sigfigs-1] \ > + str(int(newnumber[sigfigs-1])+1) > newnumber = newnumber[:sigfigs] > > # Applying the wholefigs > if wholefigs: > newnumber = newnumber[:wholefigs]+','+newnumber[wholefigs:] > exp += sigfigs-wholefigs > > # Strip zeroes to the left > newnumber = newnumber.lstrip('0') > > # print '%s 10^%d' % (newnumber, exp) > return newnumber, exp > ># Takes a string representing a number and returns a string representing ># the whole part of the number and a string representing the decimal ># part of the number. ># The number must have format [0-9]+{[.,][0-9]+}? >def numstrsep(numstr): > # Returns always two items in a list > if '.' in numstr: > return numstr.split('.')[:2] > elif ',' in numstr: > return numstr.split(',')[:2] > else: > return numstr,'' > ># Takes a number and passess it to the thousep_str function. >def thousep_num(number): > whole = str(int(number)) > decimal = str(number % 1)[2:] > return thousep_str(whole,decimal) > ># Takes strings representing a number and formats them with spaces as ># thousand (and thousandth) separators and a comma as the decimal ># separator. >def thousep_str(whole,decimal): > # Decimal part > # decimal = str(number % 1)[2:] # Only decimal part, skip "0." > if decimal and len(decimal) > 3: > nts = len(decimal)/3 > for i in range(0,nts): > inspos = 3*(i+1)+i > if decimal[inspos:] != '': > decimal = decimal[:inspos]+" "+decimal[inspos:] > # Whole part > # whole = str(int(number - number % 1)) > if len(whole) > 3: > zn = len(whole) % 3 > if zn > 0: > whole = whole[:zn]+" "+whole[zn:] > if len(whole) > 5: > nts = (len(whole)-zn)/3 - 1 > for i in range(0,nts): > inspos = (zn>0)*(zn+1)+3*(i+1)+i > whole = whole[:inspos]+" "+whole[inspos:] > > numstr = "" > if decimal: > numstr = whole+','+decimal > else: > numstr = whole > return numstr > ># The demo. ># Input as first argument the number that you want to format ># Input as second argument the number format you want your number to ># have. (You may have to input the number format in quotes so the ># terminal doesn't try to interpret the # symbols.) ># Thousand separators are hard-coded to be spaces, and decimal ># separators to be commas, as the SI standards specify. ># The first line corresponds to the number with thousands separators ># inserted. The second line corresponds to the number formatted to ># its significant figures according to the format specified. >if __name__ == '__main__': > import sys > numstr = numstrsep(sys.argv[1]) > print "%s" % thousep_str(numstr[0], numstr[1]) > # print "<%s>" % thousep_num(223987.345348576) > figs = interpret_format(sys.argv[2]) > number = sigfigs_str(numstr[0], numstr[1], figs[0], figs[1]) > valuestr = numstrsep(number[0]) > print '%s 10^%d' % (thousep_str(valuestr[0], \ > valuestr[1]), number[1])
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 51008
:
63017
| 85942