Rounding to two decimal places without Round

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Rounding to two decimal places without Round

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Rounding to two decimal places without Round

Post by cogier »

My solution would be: -

Print Int(2000.5354 * 100) / 100
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Rounding to two decimal places without Round

Post by gambafeliz »

Very good, happy my eyes. "I hope it is well translated" :)

Thank you so much.

Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Rounding to two decimal places without Round

Post 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(.....
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Rounding to two decimal places without Round

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Rounding to two decimal places without Round

Post 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
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Rounding to two decimal places without Round

Post 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!
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Rounding to two decimal places without Round

Post 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.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Rounding to two decimal places without Round

Post 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 ;)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Rounding to two decimal places without Round

Post 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?
Post Reply