Round up to first decimal

I’m finding a way to to round-up, by using 2nd decimal to round-up the first decimal as shown in below table (needed value column).
I’ve tried below method and doesn’t really gives the desired value.

((Math.Ceiling(1 * CType(correctWeight, Double)))/2).ToString
Math.Round(Math.Abs(CType(correctWeight, Double)),1).ToString

Anyone has any idea on how to achieve the needed value by rounding up?

image

@Serran_Neru

What has to display if the value is 3.92 or 3.93? Rounded to 4 or 3.9 only?

Regards,
Karthik Byggari

1 Like

Hi @KarthikByggari, it will be 4. Are you able to get it?

I doubt whether you can use any inbuilt functions, you may have to write a custom function.

@Serran_Neru

I am using custom logic to achieve (I got till 3.01 to 3.89). I wrote logic in C#. Let me add another condition for the series 3.9+ and lets see if it works.

Regards,
Karthik Byggari

Hi @KarthikByggari, yes sure. Thanks alot :slight_smile:

@Serran_Neru

I got the required output. But you have to implement the logic in UIPath because I wrote the logic in C# (tune the logic if you can) . Please let me know if you want code in C#.

Output:
image

1 Like

Hi @KarthikByggari, yes please. That would help me in someway.

@Serran_Neru

Here is the code:

        double[] values = { 3, 3.01, 3.1, 3.11, 3.2, 3.29, 3.39, 3.90, 3.91, 3.99 };
        foreach (double value in values)
        {
            //variable declaration
            string finalvalue;
            double finalvalue2;

            //seperate strings if contains "."
            if (value.ToString().Contains("."))
            {
                //split value into two parts by "."
                string[] arr = value.ToString().Split('.');

                //assign each part into one varvalue.ToString().Contains("."))iable
                int value1 = Int32.Parse(arr[0]);
                string val2 = arr[1];

                //if the decimal part is like 3.01, 3.02... 3.99
                //except 3.1,3.2.....
                if(arr[1].Length > 1)
                { 
                    //parse string into Integer
                    finalvalue2 = Int32.Parse(arr[1]);

                    //divide the number by 10
                    finalvalue2 = finalvalue2 / 10;

                    //round the value to the nearest integer
                    finalvalue2 = (int)Math.Ceiling(finalvalue2);

                    //if decimal is like 3.91 tp 3.99
                    //increment the value1
                    //for example if number is 3.91 , increment 3 to 4
                    //and set value2 to zero
                    if(finalvalue2.ToString().Length > 1)
                    {
                        value1 = value1 + 1;
                        finalvalue2 = 0;
                    }
                }
                else
                {
                    //if decimal is 3.1,3.2
                    //just consider the decimal part
                    finalvalue2 = Int32.Parse(arr[1]);                                                               
                }

                //display purpose
                if(finalvalue2 == 0)
                {
                    //if value2 is zero, just display the number without any decimals
                    finalvalue = value1.ToString();
                }
                else
                {
                    //if value2 is not zero, display number with decimals
                    finalvalue = value1.ToString() + "." + finalvalue2.ToString();
                }
                
            }
            else
            {
                //if value is 3,4,...
                //just assign value to the final string
                finalvalue = value.ToString();
            }

            //display final values in console window
            Console.WriteLine("{0} --> {1}", value,
                              finalvalue);

        }

Regards,
Karthik Byggari

Hi
Please follow this String to Integer || String to double in uipath tutorials - YouTube

hey how to write the rounded numbers as 3.10, 3.20 etc. I rounded to 2 decimal places and its writing only 3.2 3.2 etc. I need the trailing zeroes.

buddy @amithvs

if you have a variable of type double with value
in_value = 3.10
then use like this
string.format(“{0:0.00}”,in_value)

Cheers

2 Likes

Thanks.