Finding fraction from decimal

Hi, there are some decimal values 0.5,0.25,0.75 etc… I want to convert this into fractions like 1/2,1/4 etc… how to do this with UiPath.

  1. Drag and drop a “Invoke Code” activity onto your workflow.
  2. In the “Invoke Code” activity properties, set the Language to “VB.Net” or “C#,” depending on your preference.
  3. Use the following code to convert 0.5 to 1/2 and store it in a variable:
double decimalValue = 0.5;
string fraction = $"{decimalValue:N0}/{1:N0}";  // Outputs "1/2"
  1. Create a variable in UiPath to store the fraction (e.g., fractionValue).
  2. In the “Result” field of the “Invoke Code” activity, assign the fraction variable to fractionValue.
  3. You can then use the fractionValue variable in your workflow.

Hope the below steps would help you resolve this

  1. Add an Invoke Code activity to your workflow.

  2. In the “Code” section of the Invoke Code activity, you can use the following VB.NET code

Dim decimalValue As Double = 0.5  ' Replace with your decimal value 
Dim fraction AsSystem.Fraction = System.Fraction.FromDouble(decimalValue) 
Dim numerator As Integer = fraction.Numerator Dim denominator As Integer = fraction.Denominator 
Dim fractionString As String = $"{numerator}/{denominator}" 
Return fractionString

Make sure to replace the decimalValue variable with the decimal value you want to convert or u can pass that as a argument as well

  1. In the “Result” section of the Invoke Code activity, create a variable (e.g., fractionString) to store the result. The variable should be of type String to hold the fraction as a string.

  2. When you run the workflow, the Invoke Code activity will execute the code and store the fraction as a string in the fractionString variable.

Cheers @john_smith2

Hi @john_smith2
Hope this helps.

DecimalValue = 0.5
Fraction = System.Math.Round(DecimalValue, 2).ToString(“0.00”)