Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting two numbers in Python

I am coping with the formatting of numbers. I assumed that the .format allows to use multiple arguments:

a = 1.11111111111
b = 0.9
s = '({0:.2f}, {0:.2f})'.format(a, b)
print(s)

Returns:

'(1.11, 1.11)'

Instead of:

'(1.11, 0.90)'

On the other hand, this works fine:

'({}, {})'.format(a, b)

Returns:

'(1.11111111111111, 0.9)'

Any idea where is the problem?

like image 268
Karel Macek Avatar asked Feb 15 '17 13:02

Karel Macek


People also ask

What is .2f means in Python?

2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

How do you format a number in Python?

Type Specifying In Python More parameters can be included within the curly braces of our syntax. Use the format code syntax {field_name: conversion}, where field_name specifies the index number of the argument to the str. format() method, and conversion refers to the conversion code of the data type.

What is %s and %D in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42))

How to format a number for output in Python?

You want to format a number for output and you want to control the number of digits after decimal or want to add thousand separators or use exponential notations etc. To format a number in python you can use the format function.

How to format decimals in Python?

To format decimals, we will use str.format (number) where a string is ‘ {0:.3g}’ and it will format string with a number. Also, it will display the number with 1 number before the decimal and up to 2 numbers after the decimal. After writing the above code (python decimal format), Ones you will print “ f ” then the output will appear as a “ 1.34 ”.

How do you convert a string to another format in Python?

Note: You can use multiple format conversion types in a single print statement. This method was introduced in Python 3 was later also introduced to Python 2.7. Formatting is now done by calling. format () method . Syntax: ‘String here {} then also {}’.format (‘something1′,’something2’)

How to format numbers in pandas?

Formatting numbers 1 1.Restricting the number of digits after the decimal point#N#eg: if i want to restrict the value of pi ( to the 2nd or 3rd... 2 Formatting the output in pandas data-frames#N#To take it a little further, if you want to display the numbers in a... 3 Formatting the output in numpy More ...


2 Answers

You used for both of the the parameters the value a (0), you should call in the second param to value b (1).

the value before the : is for giving placeholders an explicit positional index.

This allows for re-arranging the order of display without changing the arguments.

Change

s = '({0:.2f}, {0:.2f})'.format(a, b)

To:

s = '({0:.2f}, {1:.2f})'.format(a, b)
like image 63
omri_saadon Avatar answered Sep 20 '22 19:09

omri_saadon


Values before the : character specify either a field name or a conversion; by using 0 in both cases you're essentially telling .format, via the element index, to use a in both cases.

In your second case, '({}, {})'.format(a, b), by not specifying a position, .format replaces each empty pair of {} with the elements supplied in increasing order of position.

A simple replacement, as suggested, is to use 1 to indicate that you want to use b instead of a. Alternatively, simply omit them:

s = '({:.2f}, {:.2f})'.format(a, b)

to get a similar effect.


You should skim through the Syntax for the format strings to get an idea of the rules used when formatting.

like image 43
Dimitris Fasarakis Hilliard Avatar answered Sep 17 '22 19:09

Dimitris Fasarakis Hilliard