Convert from decimal to fractional inches (string)


#1

Hi there, just wanted to share this piece of code. As I need to do this a lot it might be useful to someone who is not familiar with coding.
At first, I was using the spring nodes one that is great but I had to add a lot of nodes to it to get to this.
So this simple python node will give you a string with your fractional inches round down to a predefined approximation.

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
# Place your code below this line
# Data imput definitions
n=IN[0] #Numbers to be converted
app=IN[1] #Approximation i.E = 1/8" = 0.125 decimal. **** In Decimal ****
#The result array
r=[]
#If approximation is not defined, default it to 0.125 [1/8"]
if not app:
	app = 0.125
#Check to see if your imput have a single number or a list
if type(n) is float:
	r.append(str((n).as_integer_ratio()[0])+"/"+str((n).as_integer_ratio()[1])+"\"")

#If it is a single number
else:
	for e in n:
		b = e - int(e)
		length = len(str(e))
		round(b, 3)
		if b < app:
			r.append(str(int(e))+"\"")
		else:
			if (b%app) > 0:
				c=(int(b/app)*app).as_integer_ratio()
				d=str(c[0])+"/"+str(c[1])+"\""
				
				r.append(str(int(e))+" "+d)	

# Assign your output to the OUT variable.
OUT = r