Skip to content

Instantly share code, notes, and snippets.

@solanoize
Created August 18, 2016 21:47
Show Gist options
  • Save solanoize/77757781515110909c1077c914f26819 to your computer and use it in GitHub Desktop.
Save solanoize/77757781515110909c1077c914f26819 to your computer and use it in GitHub Desktop.
Format rupiah di Python
# rupiah.py
# ---------------

import locale

def rupiah_format(angka, with_prefix=False, desimal=2):
    locale.setlocale(locale.LC_NUMERIC, 'IND')
    rupiah = locale.format("%.*f", (desimal, angka), True)
    if with_prefix:
        return "Rp. {}".format(rupiah)
    return rupiah

Running

>>> import rupiah
>>> rupiah.rupiah_format(10000)
'10.000,00'
>>> rupiah.rupiah_format(10000, True)
'Rp. 10.000,00'
@dhanipro
Copy link

dhanipro commented Jan 25, 2022

def rupiah_format(angka, with_prefix=False, desimal=2):
    if not angka:
        return 0
    rupiah = "{:,.2f}".format(angka)
    rupiah = rupiah.replace(",", ".")
    if with_prefix:
        return f"Rp{rupiah}"
    return rupiah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment