Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

How to display decimals on Bitcoin price?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to display decimals on Bitcoin price?

by HaroldBoom on Thu May 06, 2021 6:49 pm

Hi all,

I have modified the Bitcoin example code to show another crypto price but it wont show any decimals. I can get it to display $0 and the code works on Ethereum but I would like to display the price to 2 decimals. Can anyone please help?

Code: Select all | TOGGLE FULL SIZE
 Run on Metro M4 Airlift w RGB Matrix shield and 64x32 matrix display
# show current value of Bitcoin in USD

import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal

# You can display in 'GBP', 'EUR' or 'USD'
CURRENCY = "USD"
# Set up where we'll be fetching data from
DATA_SOURCE = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?CMC_PRO_API_KEY=API_KEY_HERE&symbol=TRAC"
DATA_LOCATION = ["data", "TRAC", "quote", CURRENCY, "price"]

def text_transform(val):
    if CURRENCY == "USD":
        return "$%d" % val
    if CURRENCY == "EUR":
        return "‎€%d" % val
    if CURRENCY == "GBP":
        return ""banned character??"%d" % val
    return "%d" % val


# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]

matrixportal = MatrixPortal(
    url=DATA_SOURCE,
    json_path=DATA_LOCATION,
    status_neopixel=board.NEOPIXEL,
    default_bg=cwd + "/eth_background.bmp",
    debug=False,
)

matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(29, 20),
    text_color=0x8200EE,
    text_transform=text_transform,
)
matrixportal.preload_font(b"$012345789")  # preload numbers
matrixportal.preload_font((0x00A3, 0x20AC))  # preload gbp/euro symbol

while True:
    try:
        value = matrixportal.fetch()
        print("Response is", value)
    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)

    time.sleep(60)  # wait 3 minutes

HaroldBoom
 
Posts: 5
Joined: Sat Feb 13, 2021 11:56 pm

Re: How to display decimals on Bitcoin price?

by kevinjwalters on Thu May 06, 2021 8:01 pm

text_transform's use of %d is limiting you to integers, you want to change it to something like

Code: Select all | TOGGLE FULL SIZE
def text_transform(val, digits=2):
    if CURRENCY == "USD":
        return "$%d" % val
    if CURRENCY == "EUR":
        return "‎€%d" % val
    if CURRENCY == "GBP":
        return ""banned character??"%d" % val
    return "{:.{digits}f}".format(val, digits=digits)

kevinjwalters
 
Posts: 960
Joined: Sun Oct 01, 2017 3:15 pm

Re: How to display decimals on Bitcoin price?

by HaroldBoom on Thu May 06, 2021 8:13 pm

Hey mate, I tried the code you provided but still no luck;

Code Tried;
Code: Select all | TOGGLE FULL SIZE
def text_transform(val, digits=2):
    if CURRENCY == "USD":
        return "$%d" % val
    if CURRENCY == "EUR":
        return "‎€%d" % val
    if CURRENCY == "GBP":
        return ""banned character"%d" % val
    return "{:.{digits}f}".format(val, digits=digits)


Here is an example of the JSON from the API if that helps;
Code: Select all | TOGGLE FULL SIZE
{"status":{"timestamp":"2021-05-07T00:09:10.283Z","error_code":0,"error_message":null,"elapsed":15,"credit_count":1,"notice":null},"data":{"TRAC":{"id":2467,"name":"OriginTrail","symbol":"TRAC","slug":"origintrail","num_market_pairs":10,"date_added":"2018-01-25T00:00:00.000Z","tags":["logistics","enterprise-solutions","smart-contracts","polkadot-ecosystem"],"max_supply":null,"circulating_supply":356946525.96515,"total_supply":500000000,"platform":{"id":1027,"name":"Ethereum","symbol":"ETH","slug":"ethereum","token_address":"0xaa7a9ca87d3694b5755f213b5d04094b8d0f0a6f"},"is_active":1,"cmc_rank":267,"is_fiat":0,"last_updated":"2021-05-07T00:08:06.000Z","quote":{"USD":{"price":0.61197805024926,"volume_24h":1749282.59495921,"percent_change_1h":-0.72886033,"percent_change_24h":-8.23881359,"percent_change_7d":-2.61278268,"percent_change_30d":-22.93971646,"percent_change_60d":18.58947158,"percent_change_90d":178.90028373,"market_cap":218443439.00339934,"last_updated":"2021-05-07T00:08:06.000Z"}}}}}


Thanks for your help! I am hopeless at coding

HaroldBoom
 
Posts: 5
Joined: Sat Feb 13, 2021 11:56 pm

Re: How to display decimals on Bitcoin price?

by HaroldBoom on Thu May 06, 2021 8:29 pm

I got it to display the decimal by changing the %d to %s for a string but I cant limit it to 2 decimals.

Code: Select all | TOGGLE FULL SIZE
def text_transform(val):
    if CURRENCY == "USD":
        return "$%s" % val
    if CURRENCY == "EUR":
        return "‎€%s" % val
    if CURRENCY == "GBP":
        return ""banned character"%s" % val
    return "%s" % val

HaroldBoom
 
Posts: 5
Joined: Sat Feb 13, 2021 11:56 pm

Please be positive and constructive with your questions and comments.