Page 1 of 2
Rounding to two decimal places without Round
Posted: Thursday 1st June 2023 4:30am
by gambafeliz
Hello
I have a number that, after leaving it in two decimal places with Round, does it well, but I am interested in leaving it in two decimal places without rounding.
Example:
2000.5354 (Round, -2) =2000.54
I want: 2000.5354 = 2000.53
Ask:
Does Gambas have a way to do it, without doing:
2000 (Fixed)
0.5354 (Frac)
And then trim it with string functions.
Re: Rounding to two decimal places without Round
Posted: Thursday 1st June 2023 9:21am
by cogier
My solution would be: -
Print Int(2000.5354 * 100) / 100
Re: Rounding to two decimal places without Round
Posted: Thursday 1st June 2023 10:10am
by gambafeliz
Very good, happy my eyes. "I hope it is well translated"
Thank you so much.
Greetings.
Re: Rounding to two decimal places without Round
Posted: Thursday 1st June 2023 2:06pm
by vuott
cogier wrote: ↑Thursday 1st June 2023 9:21am
Print Int(2000.5354 * 100) / 100
There, you can also use the CInt() function.
If you want, you can also decide how many decimals to leave:
Dim f As Float = 5.123456789
Dim decimals As Byte = 6
Print Int(f * 10 ^ decimals) / 10 ^ decimals ' or CInt(.....
Re: Rounding to two decimal places without Round
Posted: Thursday 1st June 2023 8:39pm
by gambafeliz
Thanks, vuott, I'm glad to greet you.
Your proposal seemed very interesting to me since I can even determine the number of decimal places.
But I was left with honey in my mouth when you said the CInt conversion function without seeing your intention in the explanation or a code to understand you.
Re: Rounding to two decimal places without Round
Posted: Friday 2nd June 2023 12:36am
by vuott
gambafeliz wrote: ↑Thursday 1st June 2023 8:39pm
CInt conversion function without seeing your intention in the explanation or a code to understand you.
Simply...
Print CInt(2000.5354 * 100) / 100
Re: Rounding to two decimal places without Round
Posted: Friday 2nd June 2023 9:48am
by thatbruce
or perhaps, to generalize
Code: Select all
Function FixDec(floatvalue As Float, decplaces As Integer) As Float
Return Floor(floatvalue*10^decplaces)/10^decplaces
End
As usual, keep in mind Floating Point Mis-arithmetic!
Re: Rounding to two decimal places without Round
Posted: Friday 2nd June 2023 2:53pm
by gambafeliz
This question is like Christmas. Full of forum stars
Now only BruceSteers is missing and this is Christmas in full.
Well thank you, gentlemen, I found it very interesting.
Re: Rounding to two decimal places without Round
Posted: Saturday 3rd June 2023 9:47am
by BruceSteers
gambafeliz wrote: ↑Friday 2nd June 2023 2:53pm
This question is like Christmas. Full of forum stars
Now only BruceSteers is missing and this is Christmas in full.
Well thank you, gentlemen, I found it very interesting.
Haha , I have my uses
but I also know when to sit back and let those who know better have a say
I'd have just used string functions for something like this
Re: Rounding to two decimal places without Round
Posted: Saturday 3rd June 2023 11:12am
by cogier
I found this
here: -
The difference between Int() and CInt() is:
Int() may return a Float value, CInt() is limited to 32 bit Integer.
Int() rounds to the next lower value. i.e. -4.6 to -5, while CInt rounds towards 0 i.e. -4.6 to -4
How does
Int return a
Float value?