jamesbtate
jamesbtate

Reputation: 1399

Number formatting in python

How can I make this python code:

# -*- coding: cp1252 -*-
a=4
b=2
c=1.0
d=1.456
print '%fx³ + %fx² + %fx + %f = 0' %(a,b,c,d)

print like this:

4x³ + 2x² + 1x + 1.456 = 0

instead of like this (how it prints currently):

4.000000x³ + 2.000000x² + 1.0000000x + 1.456000 = 0

Upvotes: 4

Views: 11974

Answers (2)

Bertrand Marron
Bertrand Marron

Reputation: 22210

print '%gx³ + %gx² + %gx + %g = 0' %(a,b,c,d)

Upvotes: 4

neeebzz
neeebzz

Reputation: 11538

Use this > Python Decimals format

Upvotes: 2

Related Questions