Formatting numbers in view

145 views
Skip to first unread message

Gualter Portella

unread,
Oct 30, 2017, 1:59:27 PM10/30/17
to web2py-users
Dear all,

I have been trying to reformat the numbers retrieved from the DB into the format used in my country (Brazil).

I want to change decimals to ',' and thousands to '.'

The thing is that the only solution that I can figure out is in Python 3 - I am am new to programming and webdev, like this:

In the model:
   swap_separators = { ord('.'):',', ord(','):'.' }

In the view:

   {{ ativos = format(float(table.column), '0.2f') }}

  {{=format(float(ativos), ',').translate(swap_separators)}}


I am currently working with Python 3. My question is: will this  solution work when I deploy the app in a host that uses Python 2 for web2py?

If someone else has asked or posted a solution for this before, please direct me to it. Otherwise, any help is really appreciated.

Cheers,


Paolo Caruccio

unread,
Oct 30, 2017, 5:16:40 PM10/30/17
to web2py-users
If it is feasible for you, you could use Intl.NumberFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) javascript Internationalization API.

Desobediente

unread,
Oct 30, 2017, 5:47:34 PM10/30/17
to web...@googlegroups.com
Cross posting my answer in the brazilian list

money string formatting in python2

currency = str('U$$')
value = float(0.02)
formatted_value = '{:,.2f}'.format(value)
print(' '.join([currency, formatted_value]))

cryptocurrency formating (8 decimals)

currency = str('BTC')
value = float(0.00000327)
formatted_value = '{:,.8f}'.format(value)
what_to_print = ' '.join([formatted_value, currency])

Gualter Portella

unread,
Oct 30, 2017, 7:04:16 PM10/30/17
to web...@googlegroups.com
Thank, Paolo! I will take a look at the API.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Us-pjfme8zY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Gualter Portella
Reply all
Reply to author
Forward
0 new messages