yasmikash
yasmikash

Reputation: 157

How to expand a number in scientific notation in Jinja2?

So, let's say I have a long floating-point number stored in my database (ex: 0.00000003), but Jinja2 displays this number in scientific notation, which is bit awkward for what I'm expecting with my flask app. Is there any way to directly expand numbers in scientific notations in Jinja2, as we do in python using format:

number = 3e-08     
format(number, '.8f')

Upvotes: 0

Views: 1425

Answers (1)

rfkortekaas
rfkortekaas

Reputation: 6474

You are already giving the answer yourself. Use format on the number before you pass it to the template. Or use format in the template.

from flask import Flask, render_template_string

app = Flask(__name__)

number=3e-08

with app.app_context():
    render_template_string('{{s}}', s=format(number, '.8f'))

    render_template_string('{{ "%.8f"|format(d)}}', d=number)

Upvotes: 1

Related Questions