It is necessary to display the number a of type float on the screen with an accuracy of at least n decimal places.
Python 3, without the use of libraries.
|
4 answers
n = 5 a = 1.2345678912345 template = '{:.' + str(n) + 'f}' print(template.format(a)) - 2Alternatively, there is still a function
format(and not a method for strings):format(2.123456789, ".5f"). Possible arguments for formatting are described at https://docs.python.org/3/library/string.html#formatspec - m9_psy - The solution is acceptable, but you need to get an answer with an accuracy of at least n digits after the decimal point. Therefore, it is not necessary to remove extra decimal places - the more accurately the better :) - Sithell
- oneAnd then it is not clear how to limit the output. After all, because of the binary representation, no one guarantees that after the last number entered after the comma there will be only zeros. For example, in the same example, you can change n to 25, and look at the result. - Jenyay
- @ Jenyay, 1) About 25, well, so this is your mistake)) want to say that 1.234566678912345 is not equal to 1.23456789123450000000000000? 2) How is this limiting output? There is a finite rational number of type float. If the number of decimal places is greater than n, simply output a. - Sithell Nov.
- @Sithell: it’s too late now (this makes the existing answers wrong), but the words “the more accurate the better” should be put in the question. Now your question is read what you want
2to2.000..turn (0repeatedntimes). If you need an exact result, then justrepr(a)call (this is not a fixed format). Formally, sincen <= ntrue, any answer that returnsndigits returns at leastndigits. - jfs
|
Python 3.6+:
>>> a = 2 >>> n = 3 >>> f'{a:.{n}f}' '2.000' Or (on earlier versions):
>>> '{a:.{n}f}'.format(**vars()) '2.000' |
def rounds(num, max_=2): '''с точностью не более n "значащих цифр", после запятой. ''' left, right = str(num).split('.') zero, nums = zero_nums = [], [] for n in right: zero_nums[0 if not nums and n == '0' else 1].append(n) if len(nums) == max_: break return '.'.join([left, ''.join(zero) + ''.join(nums)]) print(rounds(0.0102345, 3)) >>>0.0102 print(rounds(0.000102345, 4)) >>>0.0001023 - eeem, and from what you have taken what you need with an accuracy of "no more than n"? After all, I even singled out in the question in bold "NOT LESS" - Sithell
- tk with an accuracy of at least n, this is strange. And if after the comma less than n characters, add zeros? And if it is more, then it is not necessary to do anything, since the condition is satisfied automatically. - vadim vaduxa
- Yes, you understood the question correctly - Sithell
|
Here is the solution I get:
>>> a = 2.0 >>> n = 5 >>> print(str(a).ljust(len(str(int(a)))) + n + 1, '0') 5.00000 or
>>> a = 2.12345678 >>> n = 5 >>> print(str(a).ljust(len(str(int(a)))) + n + 1, '0') 2.12345678 - The number of brackets is wrong (syntaxError). Even if you add a bracket, it returns 2000000 for
a,n=2,5instead of the expected'2.00000'. For numbers of type1e-10returns'1e-1000'(which is hardly the desired result). - jfs
|