Search code examples
numbersracketroundingdigits

How to round the numbers up to the third digit?


I havent found a solution for my code to round the outcome numbers up to the 3rd digit. For example: 1,235

  (round (/ (* (cpu-clock cpu) (cpu-cores cpu)) (cpu-price cpu)))

I found this in a tutorial but I expect a same solution for a decimal integer. How can I do it?

(real->decimal-string n [decimal-digits]) → string?

Solution

  • A simple language-agnostic solution is to multiply by 10^digits before rounding, and then divide again after. So if you want to keep 3 digits, multiply by 1000:

    round(number * 1000) / 1000
    

    If the number is 1.23534, it gets multiplied to 1235.34, rounded to 1235, and then divided back down to the answer you hope for: 1.235