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

Re: Rounding to two decimal places without Round

Post by gambafeliz »

BruceSteers

I was done the same, was used string functions. That's why you ask the question. :)

On the other hand, Cogier you have managed with your last reflection to make my neurons a mess. But what question are you asking?
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 »

On the other hand, Cogier you have managed with your last reflection to make my neurons a mess. But what question are you asking?
Sorry, I was not trying to fry your brain! :D

The purpose of Int is to create an Integer. So how can it return a Float?
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
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: Sunday 4th June 2023 1:39pm The purpose of Int is to create an Integer. So how can it return a Float?
Uhmmm... perhaps the question should be put in Gambas "Mailing List".
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 »

Int() can return a float value because...
Dim fNum as Float
fNum=Int(1.234)
Print fNum

1.0000

whereas

Dim fNum as Float
fNum=CInt(1.234)
Print fNum

!@OUrg Expected float got Integer

What is misrepresented here is f(x) returns a value that can be interpreted as a variable on the left hand side of the assignment and therefor be a valid assignment.
Think this way, if fNum was a Float[] then you would not expect fNum=Int(1.234) to work but fnum=[Int(1.234)] should. There are many similar cases where the assignment could be misunderstood as being a nonnegotiable translation but in fact they are. One of the beauties of this is that an Object can in fact be anything so if the assignee is an object then the assignment can work.
Have you ever noticed that software is never advertised using the adjective "spreadable".
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 »

thatbruce wrote: Monday 5th June 2023 11:42am
What is misrepresented here is f(x) returns a value that can be interpreted as a variable on the left hand side of the assignment and therefor be a valid assignment.
Think this way, if fNum was a Float[] then you would not expect fNum=Int(1.234) to work but fnum=[Int(1.234)] should. There are many similar cases where the assignment could be misunderstood as being a nonnegotiable translation but in fact they are. One of the beauties of this is that an Object can in fact be anything so if the assignee is an object then the assignment can work.
I can't get my head around this. My brain hurts now. :?
Post Reply