3

The title might confuse you! Don't worry, I have explained it here:

As an example, the convert() function should return 11.098 if I pass convert(11098,3). I am passing the convert(11098,2), so convert() should return 110.98.

Here, any integer number should be converted to a float by adding decimal points based on the given fractions. Based on fractions, it should add decimal points.

Is there any mathematical formula to convert this? I can implement them in my python code.

5 Answers5

2

Here is an easy solution that you can understand -

def convert(number,points):
    decimal = pow(10,points) # power function

    return number / decimal

num = convert(11098,3)
print(num)

So here, the decimal variable determines the number of points to divide the number.

Result-

11.098

Another Example-

num = convert(11098,5)
print(num)
# Output: 0.11098
PCM
  • 2,881
  • 2
  • 8
  • 30
  • However, it roundoff the original value in my case: --------------------------------------------integer : 1002008009010005005020000001014004000021004001025------------------------------------------------------------------------------------------------------------------------------decimal : 0.001002008009010005--------------------------------------------------------------------------------------------------------------------------------------------------------------------it should be like: 0.1002008009010005005020000001014004000021004001025---------------why? is it so, – Abhijeet Mar 12 '23 at 20:20
2

Here, we can use little bit of Maths.

As we know, any number when divided by power of 10, the decimal point shift to the left that many number of times as there are 0 in the divisor.

We can raise the divisor 10 to the power the user provides.

That way, you will get the desired output.

def convert(x,d):
    return int(x) / 10**d

print(convert(11098,2))
1

I'm not too sure if there's already a function that does this readily available, but you could do this:

def convert(value, decimals):
     value = str(value) # convert value to string-type for indexing
     d1 = value[:-decimals] # d1 comes before the decimal
     d2 = value[-decimals:] # d2 comes after the decimal
     output = f"{d1}.{d2}" # join d1 and d2 with . using f-string
     return output

print(convert(11098, 2))

110.98

Note that the output will also be string-type, you can change it to float-type if needed by using return float(output) instead.

Hopefully that helps!

If my string manipulation was unclear, you can read this article I posted on LearnDataSci for more information: https://www.learndatasci.com/solutions/python-substring/

Alfie Grace
  • 222
  • 1
  • 9
0
import math
def convert(num,power):
return (float)(num/math.pow(10,power))

math.pow(x,y) function returns x^y
To move the decimal places we take the power of 10 and using that we can move the decimal places

  • Please provide an explanation of why this code works. Simply posting the code, is not good. Also, you need to format your code, see [how](https://stackoverflow.com/editing-help)? – PCM Jul 23 '21 at 13:14
0

You can try this with some python lambda function.

convert = lambda I, F: I/ 10**F
print(convert(11098, 3))
# output:
# 11.098
Vincent55
  • 33
  • 3