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.
- Drag and drop a “Invoke Code” activity onto your workflow.
- In the “Invoke Code” activity properties, set the Language to “VB.Net” or “C#,” depending on your preference.
- 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"
- Create a variable in UiPath to store the fraction (e.g.,
fractionValue
). - In the “Result” field of the “Invoke Code” activity, assign the
fraction
variable tofractionValue
. - You can then use the
fractionValue
variable in your workflow.
Hope the below steps would help you resolve this
-
Add an Invoke Code activity to your workflow.
-
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
-
In the “Result” section of the Invoke Code activity, create a variable (e.g.,
fractionString
) to store the result. The variable should be of typeString
to hold the fraction as a string. -
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”)