0 0 Votes

Delphi/Lazarus: Round Number to the next 10, 100 or 1000

Question by Guest | 23/01/2016 at 13:58

I have already read the tutorial about how to round to a specific number of decimal places in Delphi and Lazarus

However, I am now interested in rounding before the decimal separator (comma or digit) instead of after it. I mean, I would like to round to the tens, hundreds or thousands digit of a number. For example, I would like to turn 12345 to 12340, 12300 or 12000.

Is there also a function available for that?

ReplyPositiveNegative

Delphian

Show Profile | Message
Avatar
2Best Answer
2 Votes

Yes, that is a function for that and you can even use the same function like you have used for rounding decimal places.

Here is an example:

x := 12345.12345;
k := RoundTo(x, -3);  // k = 12345.123
k := RoundTo(x, -2);  // k = 12345.12
k := RoundTo(x, -1);  // k = 12345.1
k := RoundTo(x,  0);  // k = 12345
k := RoundTo(x,  1);  // k = 12340
k := RoundTo(x,  2);  // k = 12300
k := RoundTo(x,  3);  // k = 12000
k := RoundTo(x,  4);  // k = 10000

Depending on whether you are passing a positive or negative value to the function RoundTo, it is rounded before or behind the decimal separator. So, in order to round to ten, you have to pass 1. If you would like to round to hundred, you have to pass 2. Accordingly 3 for the thousands digit.

Important: To make it work, you have to add the unit Math. and if you do not want to use this unit, here are some more ideas to solve this task.
23/01/2016 at 14:14

ReplyPositive Negative
Reply

Related Topics

PHP: Rounding Numbers
Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.