This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 10 months ago.
How to print the calculation of float with only 3 more decimal
If I calculated something and the answer of that calculation is like, 3.33333333...
I just want to print it like 3.333 and that's it
With the round function:
round(3.33333333, 3)
You can use format specifiar
num = 3.3333333
print("%.3f"%num)
Output:3.333
a=1.233455543323
print('{0:.3f}'.format(a))
output=1.233
you can use round function
syntax:
round(number, digits)
number: Required. The number to be rounded
digits: Optional. The number of decimals to use when rounding the number. Default is 0
num = 3.33333333
print(round(num, 3))
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to round Value based on certain conditions, please check below.
When Value comes in like
21.43 round to - 21
21.65 round to - 22
21.5 round to - 21.5
Use the round function plus an if statement:
myNum = 21.43
if mynum % 1 != 0.5:
myNum = round(myNum)
You can utilize the round function
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number
As an example, round(21.43) returns 21.
This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Closed 3 years ago.
I want to round some int numbers but I came across with the strange feature of round() for example
round(2.1) = 2
round(2.5) = 2 #it rounds to ceil
round(2.7) = 3
it rounds differently with the odd number as follow
round(5.1) = 5
round(5.5) = 6 #it rounds to floor
round(5.7) = 6
it rounds the X.5 to the floor with the x = even numbers but with the X = odd numbers it rounds to the ceil
I want to ask what is the advantage of this round? and where can I use it in our examples ? or what is its usage?
Looks like if it's close it goes to the even option.
From the documentation https://docs.python.org/3/library/functions.html#round
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.
For a general Python object number, round delegates to number.round.
Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.
This question already has answers here:
How to check last digit of number
(10 answers)
Closed 4 years ago.
I was wondering if there was any way to get the ones place of a integer. For example, if I had the number 41, would there be any easy way to get 1 from that? Thanks!
Take modulo by 10
a = float(input('Enter a number : '))
print( a % 10)
Modulo operator return the remainder of number so any number modulo with 10 will return its ones number for more https://docs.python.org/3/reference/expressions.html
This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 5 years ago.
For example
x = 1.23
print("%.3f" % x)
Output will be "1.230" and I want "1.23".
Is there any way to do? Thank you.
edited:
I want a function that will print floating point at limit precision and will not print zero followed if it's not reach the limit given
If you want to output up-to 3 digits but without 0 you need to format to 3 digit and rstrip zeros:
for n in [1.23,1.234,1.1,1.7846]:
print('{:.3f}'.format(n).rstrip("0")) # add .rstrip(".") to remove the . for 3.00003
Output:
1.23
1.234
1.1
1.785
Have a quick read here for python 3 formattings: https://pyformat.info/#number or here: https://docs.python.org/3.1/library/string.html#format-examples
This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 7 years ago.
I am trying to calculate some performance metrics as [RATE] in SQL but no matter what I do I am only getting integer values in return.
In my dataset I have a Numerator and a Denominator both stored as integers that are broken down into groups and subsets. The rate is calculated slightly differently depending on the group and subset. For group 1 the calculation is simply N/D. For group 2 the calculation is (N*1000)/D with the exception of 1 subset which is calculated (N*10000)/D.
I wrote the query as:
SELECT [Group]
,[SubSet]
,[Numerator] N
,[Denominator] D
,CASE WHEN D=0 Then NULL
WHEN [Group]='G1' THEN [N]/[D]
WHEN [SubSet]='S2' THEN ([N]*10000)/[D]
WHEN [SubSet] NOT LIKE 'S2%' AND [G] NOT LIKE 'G1%' THEN ([N]*1000)/[D] as [RATE]
No matter what I do the outcome variables are integers. I tried formatting RATE as varchar, decimal, and float with no success. I tried changing N and D's format to varchar, decimal, and float as well. I tried changing the equations from (N*1000)/D to (N/D)*1000 but still no effect.
What am I missing/doing wrong?
The problem you are having is because SQL is doing integer division, which will only return whole numbers. To get a decimal return value you must have at least one value as a decimal.
Try this:
(CAST([N] as decimal(12,6))/[D]) * 1000
Adjust decimal(12,6) based on the precision you are expecting. 12,6 will return a decimal with 6 digits after the decimal point. If you wanted only 2 decimal places use 16,2.
If you then want to round the calculated value you will need to make use of the ROUND function in SQL.
Round to the second decimal place:
ROUND((CAST([N] as decimal(12,6))/[D]) * 1000, 2)
You need to use CAST:
CAST ((N*1000) AS FLOAT) / D
Hope this helps.
SELECT (n * 1000.0) will do it.