0

Sorry for the weird title, but I don't really know how to explain this. Basically, I have this code that grabs from an API, however, if the numeric value is trying to display low. (Let's say 0.0008 or lower) it will display as a bunch of numbers and an e at the end.

Example: 8.888e-5 or something random.

How can I make it display as a number? Like: 0.00084BTC per PLSR.

(Python 3.6.4) It MUST Be kept as a String to work!

Why this is not a Duplicate: Although another thread found a similar answer to what I want, it doesn't exactly explain how to incorporate it into my code where I have already converted it into a string. Would I just overlay it overtop after its been converted to a string or..?

Code:

import requests
import discord
import asyncio

url = 'https://cryptohub.online/api/market/ticker/PLSR/'
response = requests.get(url)
data = response.json()['BTC_PLSR']

client = discord.Client()

@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')

price = print('PLSR Price:', data['last'])
pulsar = str(data['last'])

await client.change_presence(game=discord.Game(name="PLSR Price: " + pulsar))
Blake Xavier
  • 75
  • 5
  • 13
  • Possible duplicate of [How do I suppress scientific notation in Python?](https://stackoverflow.com/questions/658763/how-do-i-suppress-scientific-notation-in-python) – Nelson Feb 14 '18 at 05:33

2 Answers2

3

You are seeing scientific notation. 8.888e+2 means 8.888x102.

You need to use the String Formatting Mini-Language if you don't like the defaults. Python has gone through some gyrations with formatting. Here are some options:

>>> value = .000008888  
>>> value                           # default display of small number
8.888e-06
>>> print('%.8f' % value)           # old, deprecated format.
0.00000889
>>> print('{:.8f}'.format(value))   # newer format.
0.00000889
>>> print(f'{value:.8f}')           # newest format in Python 3.6.
0.00000889

Note that .8f means "8 places after decimal, fixed floating point".

More information:

Per your comment, you could format a string in scientific notation with:

>>> pulsar = '8.888e+2'
>>> f'PLSR Price: {float(pulsar):.2f}'
'PLSR Price: 888.80'
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • This helps explain things but how would I implement it into my code? It can't be done this way since I converted a List to a String, and doing this gives me errors. – Blake Xavier Feb 14 '18 at 06:03
  • @Blake Please show a simple example, not a link. Links break and the question won't be useful to future viewers. See [mcve] guidelines. To add code, cut-n-paste into the question, highlight it, and use the `{}` button to format as code. – Mark Tolonen Feb 14 '18 at 06:05
  • Added it, although it was really hard to figure out. – Blake Xavier Feb 14 '18 at 06:10
  • await client.change_presence(game=discord.Game(name=f'PLSR Price: {float(pulsar):.2f}')) - This won't return in the correct format. Did I do something wrong? – Blake Xavier Feb 14 '18 at 06:16
0

When numbers get big or small enough, Python refers to them using scientific notation. This doesn't mean the value is incorrect or "weird", it's just displaying in a different format.

To force it to display, for example, 8 digits, you would need to format it to do so:

print('%.8f' % value)
tverghis
  • 957
  • 8
  • 31
  • Getting Error: Must be a Real Number, not str – Blake Xavier Feb 14 '18 at 05:43
  • @BlakeXavier convert your string to a float first: `print('%.8f' % float(value))` – tverghis Feb 14 '18 at 05:50
  • @BlakeXavier remove the `print` and the surrounding `()` in this line: `x = print ('%.8f' % float(pulsar))`. You are also converting from (presumably) a `float` (`data['last']`) to a `string`, and back to a `float` now. Skip converting it to a `string`. – tverghis Feb 14 '18 at 06:08
  • @BlakeXavier please follow Mark's answer, he has exactly what you need. – tverghis Feb 14 '18 at 06:11