1

Is there a way to round always up to the next tens? E.g., 0.000000000003 shall become 0.1. 0.1244593249234 shall become 0.2 and 0.9x shall become 1. Negative numbers are not a thing here.

Is there a built in or do I need to come up with something?

newbypyth
  • 91
  • 1
  • 1
  • 8
  • there is a way to probably come up with something that is not built in – Matiiss Apr 03 '21 at 21:01
  • I could just hardcode it in.. But I thought there might be a handy function, no? – newbypyth Apr 03 '21 at 21:02
  • don't know about any builtins – Matiiss Apr 03 '21 at 21:02
  • @ArcKoor Nope, that rounds to 0, it shall round to the next bigger tens – newbypyth Apr 03 '21 at 21:04
  • Are you only dealing with numbers in the (0, 1) range, or would you also want 123.4567 to become 123.5? – Reti43 Apr 03 '21 at 21:05
  • @Reti43 Only 0, 1 range – newbypyth Apr 03 '21 at 21:05
  • @newbypyth I just realised. You could check out [this](https://kodify.net/python/math/round-decimals/#round-decimal-places-up-in-python), looks like someone already has done what you need. – arckoor Apr 03 '21 at 21:06
  • 1
    import the `math` module and run the ceiling function, e.g.: `math.ceil(x*10.0)/10.0`. You need to add the multiplication/devision workaround to ceil to the desired digit, since ceil will round up to the nearest integer – blubase Apr 03 '21 at 21:07
  • (1) You are rounding to *tenths*, not *tens*. (2) See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You apparently didn't do *any* research before posting your question. – Prune Apr 03 '21 at 21:07
  • For range (0,1) the ceil from math will do. You can just use math.ceil(x*10)/10 to round given x always up to the 1st decimal digit. – STerliakov Apr 03 '21 at 21:07
  • https://docs.python.org/3/library/decimal.html#module-decimal and https://docs.python.org/3/library/decimal.html#decimal.ROUND_CEILING – Pedro Lobito Apr 03 '21 at 21:09
  • Here is a way of doing ```x = 0.1244593249234 print("{:.1f}".format(x+.1))``` – Buddy Bob Apr 03 '21 at 21:11
  • I've reopened this question because isn't a dupe. – Pedro Lobito Apr 03 '21 at 21:13
  • @BuddyBobIII this does not account for when the number is already a tenth for example: `x = 0.2` – Matiiss Apr 03 '21 at 21:22
  • @Matiiss works fine for me... I get .3 – Buddy Bob Apr 03 '21 at 21:25
  • @BuddyBobIII except it is not correct since there is nothing to round – Matiiss Apr 03 '21 at 21:31
  • @Matiiss It works fine. I get .3 with this line. Yes it won't hurt to make an exception for If the float only has 1 decimal point. But that's pointless. – Buddy Bob Apr 03 '21 at 21:33
  • @BuddyBobIII that is certainly not pointless and it really does not take that much more code – Matiiss Apr 03 '21 at 21:33
  • ... It works leave it – Buddy Bob Apr 03 '21 at 21:34
  • @BuddyBobIII not to sound rude (hope You don't mind) I improved Your code for that one little exception – Matiiss Apr 03 '21 at 21:38

3 Answers3

1

by multiplying the input f to 10, you will map it from [0, 1] to [0, 10]. Now by getting ceil from this number, you will get an integer number in [0, 10] that by dividing it by 10, you will obtain the next ten in [0, 1] range as you desire:

import math

next_ten = math.ceil(f * 10.0) / 10.0
OmG
  • 18,337
  • 10
  • 57
  • 90
1

Idk, it's a simple solution. I tested it out for all the options you gave and seems to work properly.

x = 0.2
print("{:.1f}".format(x+.1))
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
  • This is an important answer. In most cases, you want to STORE your data in its actual form, and you only want to rounding or truncation when you DISPLAY the data. Remember that `0.2` cannot be represented exactly in floating point. It's always going to be an approximation. – Tim Roberts Apr 03 '21 at 21:30
  • Given `x = 0.26`, this prints `0.4`. I doubt that that's what the OP wants. (And `0.65` produces `0.8`.) – Mark Dickinson Apr 04 '21 at 11:10
0

here ya go. this is not built in and probably too excessive but it should work at least, work is done:

number = input()

number = number.split('.')
first = int(number[-1][0])

if first == 9:
    number = str(int(number[0]) + 1)
else:
    for digit in number[-1][1:]:
        if digit != '0':
            first += 1
            break

    number = number[0] + '.' + str(first)

print(number)

Matiiss
  • 5,970
  • 2
  • 12
  • 29