Write a Python program to print the following floating numbers upto 2 decimal places with a sign


The code is a Python program that demonstrates the use of format specifiers in string formatting. The program takes two floating-point numbers, a and b, as input and then formats their values for display. The first line of code assigns the value -84.99 to the variable a, and the second line assigns the value 23.36778 to the variable b.

The program then uses the print() function to display the original values of a and b. The format of the values is determined by the format specifier within the curly braces {} in the string passed to print(). The format specifier :+.2f used in the format string specifies that the value should be formatted as a floating-point number with two decimal places. The + sign before the .2f tells the formatter to include a plus sign for positive numbers. Finally, the program prints the formatted values of a and b.

Source Code

a =-84.99
b = 23.36778
print("Original Value A : ", a)
print("Formatted Value A : "+"{:+.2f}".format(a));
print("Original Value B : ", b)
print("Formatted Value B : "+"{:+.2f}".format(b));

Output

Original Value A :  -84.99
Formatted Value A : -84.99
Original Value B :  23.36778
Formatted Value B : +23.37


Example Programs