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.
Rounding to two decimal places without Round
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Rounding to two decimal places without Round
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Rounding to two decimal places without Round
My solution would be: -
Print Int(2000.5354 * 100) / 100
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Re: Rounding to two decimal places without Round
Very good, happy my eyes. "I hope it is well translated"
Thank you so much.
Greetings.
Thank you so much.
Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Re: Rounding to two decimal places without Round
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.
Amare memorentes atque deflentes ad mortem silenter labimur.
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Re: Rounding to two decimal places without Round
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.
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.
Re: Rounding to two decimal places without Round
Simply...gambafeliz wrote: ↑Thursday 1st June 2023 8:39pm CInt conversion function without seeing your intention in the explanation or a code to understand you.
Print CInt(2000.5354 * 100) / 100
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
Re: Rounding to two decimal places without Round
or perhaps, to generalize
As usual, keep in mind Floating Point Mis-arithmetic!
Code: Select all
Function FixDec(floatvalue As Float, decplaces As Integer) As Float
Return Floor(floatvalue*10^decplaces)/10^decplaces
End
Have you ever noticed that software is never advertised using the adjective "spreadable".
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Re: Rounding to two decimal places without Round
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.
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.
- BruceSteers
- Posts: 1569
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Rounding to two decimal places without Round
Haha , I have my uses but I also know when to sit back and let those who know better have a saygambafeliz 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.
I'd have just used string functions for something like this
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Rounding to two decimal places without Round
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?
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?