×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Locale number formatting

    Hi all,
    I'm using the following python code to output formatted float numbers:

    Code:
    print("%1.2f"%12345.54321)
    On a 3rd edition Symbian with the italian language chosen, I get the following formatted string:

    Code:
    "12.345.54" (it should be "12.345,54" for the italian locale, or "12,345.54" for the English locale)
    The problem is that while the thousand separator is a dot like it should, the decimal separator is a dot instead of a comma, so the number is wrong (and it's unreadable too)

    Since pys60 doesn't support the locale module, is there a way to get formatted numbers right?

    TYIA

  2. #2
    Regular Contributor
    Join Date
    Dec 2007
    Location
    India
    Posts
    133

    Re: Locale number formatting

    Hi,

    Try this, it may be useful

    Code:
    v = 1.23456789
    print ("%4.2f"%v).replace(",", ".")
    appuifw.note(u"Number: "+str(("%4.2f"%v).replace(",", ".")), 'info')
    Last edited by kandyfloss; 2008-04-10 at 19:19. Reason: Corrected code
    Kandyfloss

    V 7.0642.0
    18-10-06
    RH-51
    Nokia 7610

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Re: Locale number formatting

    Quote Originally Posted by kandyfloss View Post
    Hi,

    Try this, it may be useful

    Code:
    v = 1.23456789
    print ("%4.2f"%v).replace(",", ".")
    appuifw.note(u"Number: "+str(("%4.2f"%v).replace(",", ".")), 'info')
    Thanks but my problem is a bit different: I get a dot as a delimiter for both the thousands and the decimals, which is wrong. I should get a dot for thousands and a comma for decimals, or the opposite, but not the same sign!

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Locale number formatting

    I'm not sure if this works as I don't get your error on my phone, so I'm sorry if it doesn't work.
    Try this:

    Code:
    #Store the number as a string
    n="12345678910.9028345"
    #Find the index of the ".", or ","
    i=n.index(".")
    
    #Put each group of 3 digits of the integer part in a list
    g=[]
    a=i
    while(a>0):
    	g.append(n[a-3:a])
    	if(i-3<0):g.append(n[0:i])
            i-=3
    
    #Delete an empty group (don't know why it occurs)
    del g[len(g)-2]
    
    #Add the groups to a new string, each followed by "," (or ".", if you want)
    j=len(g)-1
    n2=""
    while(j>=0):
    	n2+=g[j]+","
    	j-=1
    n2=n2[0:len(n2)-1]
    
    #Add the remaining fractional part
    n2+=n[i:len(n)]
    
    #For this example, this should return 12,345,678,910.424548693
    Hope this helps

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Re: Locale number formatting

    Thank you Bogdan. I already did it a slightly different way than you.

    Anyway now I don't know if the behaviour I get from the python interpreter on my phone is to be considered a bug to be fixed.

    Quote Originally Posted by bogdan.galiceanu View Post
    I'm not sure if this works as I don't get your error on my phone, so I'm sorry if it doesn't work.
    Try this:

    Code:
    #Store the number as a string
    n="12345678910.9028345"
    #Find the index of the ".", or ","
    i=n.index(".")
    
    #Put each group of 3 digits of the integer part in a list
    g=[]
    a=i
    while(a>0):
    	g.append(n[a-3:a])
    	if(i-3<0):g.append(n[0:i])
            i-=3
    
    #Delete an empty group (don't know why it occurs)
    del g[len(g)-2]
    
    #Add the groups to a new string, each followed by "," (or ".", if you want)
    j=len(g)-1
    n2=""
    while(j>=0):
    	n2+=g[j]+","
    	j-=1
    n2=n2[0:len(n2)-1]
    
    #Add the remaining fractional part
    n2+=n[i:len(n)]
    
    #For this example, this should return 12,345,678,910.424548693
    Hope this helps

  6. #6
    Regular Contributor
    Join Date
    Dec 2007
    Location
    India
    Posts
    133

    Re: Locale number formatting

    The code I provided works for me, it replaces the dot(.) with the comma in the note.
    Glad you found a way out!
    Kandyfloss

    V 7.0642.0
    18-10-06
    RH-51
    Nokia 7610

  7. #7
    Regular Contributor
    Join Date
    Sep 2005
    Location
    Finland, Helsinki
    Posts
    323

    Re: Locale number formatting

    Quote Originally Posted by palple View Post
    Anyway now I don't know if the behaviour I get from the python interpreter on my phone is to be considered a bug to be fixed.
    It is a closed bug with "wont fix" flag:
    http://sourceforge.net/tracker/index...55&atid=790646

    I lost some GPS track data yesterday because of this nonstandard formatting:
    altitudes over 1000 meters were formatted (in Finnish locale) like
    1000 -> 1 000
    2000 -> 2 000
    etc.

    In the server those strings were casted back to float in a PHP-script like this: "(float)$alt" and everything after the first thousand delimiter were lost.

    Now I use this workaround:

    Code:
    tp_data = {
                "lat":(u"%.7f" % (p.lat)),
                "lon":(u"%.7f" % (p.lon)),
                "alt":(u"%.1f" % (p.alt)),
                "spd":(u"%.2f" % (p.speed)),
                "crs":(u"%.2f" % (p.course)),
                "time":isotime,
                }
    # <KLUDGE> #
    # Pys60 float formatting BUG
    # There should not be any whitespaces in any of the value
    # NOTE: this won't fix if thousand separator is e.g. '.'
    for k in tp_data: tp_data[k] = tp_data[k].replace(" ", "")
    # </KLUDGE> #
    Last edited by aaaaapo; 2008-04-28 at 16:44. Reason: Removed comment about re-opening this bug, it is already mentioned in pys60 1.4.3 release_notes_1_4_3.txt's "Known issues"

Similar Threads

  1. WML & WMLSCript problem ????
    By gemini_shooter in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2006-01-14, 06:02
  2. WML & WMLSCript problem ????
    By gemini_shooter in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2006-01-14, 06:01
  3. HELP - RCall - Dial voice number crash
    By cmryan in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2004-02-13, 14:53
  4. Personal
    By JSmith77 in forum PersonalJava
    Replies: 4
    Last Post: 2003-07-30, 12:52
  5. Unicode Song Title In NSM Ringtone
    By mcai7db3 in forum Smart Messaging
    Replies: 3
    Last Post: 2002-08-28, 06:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×