Converting Text With Number()

So far, the port of batch_plan.py has used 37.5 as a number. The real script gets it as text: each evening, the head roaster pastes ORDER_KG_TEXT = "37.5" from the web shop’s export, and float() converts it. That paste is the one part a person types, so it’s where things go wrong.

Convert the Pasted Order

The planner uses Number for ordinary whole and decimal values. Number(text) is a starting point for these conversions, not an exact replacement for every float(text) or int(text) call. Like float(), it converts the whole text or nothing and allows surrounding whitespace. The accepted spellings differ in places, though, and Number() reads "0x10" as 16.

Unlike int(), it accepts "37.5". Number.isInteger() checks whether the converted value is whole, not whether the text uses Python’s integer syntax; "3.0" still passes. Large integer text can also lose precision before this check.

One evening the order comes from a café’s email, which uses a decimal comma, so the paste is "37,5". Python stops on the conversion line with ValueError: could not convert string to float: '37,5'. Here’s the port, leaving out the rounding and formatting:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.