I want to make a function that prints accurate result(including the numbers after decimal) when the right value after the decimal is anything but 0.
For example,
if num = 5.9999947575, return 5.9999947575
if num 50000.0000, return 50000
if num = 500.0000023, retutn 500.0000023
if num 7215.22152790009999, return 7215.22152790009999
if num = 1000.0000 return 1000
There might be a much simpler way, but I would write a function that turns the number into a string, loop through each character, if it is after the decimal point, and not a zero, round it off.
I also read read somewhere that you can split the number in two parts one before the decimal and one after the decimal. Then you can check the right values of the decimals for the zeros. But its not working
The problem is when the returned price (answer) is in natural numbers, it works fine.
But when the returned price is a float (say 502.1489), the output gets rounded off (it becomes 502).
I want the output price to be what it is when it is in float( exact 502.1489). But if the output is (502.0000) I want it to be 502.
In simple terms, only shorten it when the decimal values are 0 else keep the way it is.
Please let me know if you guys understand me. I will explain my problem again if you want.
Yeah, sure. The problem is when the returned price (answer) is in natural numbers, it works fine.
But when the returned price is a float (say 502.1489), the output gets rounded off (it becomes 502).
I want the output price to be what it is when it is in float( exact 502.1489). But if the output is (502.0000) I want it to be 502.
In simple terms, only shorten it when the decimal values are 0 else keep the way it is.
Is there more code than this? Because nothing there is rounding anything - how is 502.1489 coming out as 502? Also, surely 5.9999947575 can’t be a valid answer, as it specifies that the answers have to be in Rupia?