Rounding to two decimal places without Round
Posted
#1
(In Topic #1052)
Enthusiast

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.
Posted
Posted
Enthusiast

Thank you so much.
Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Regular

There, you can also use the CInt() function.cogier said
If you want, you can also decide how many decimals to leave:
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Enthusiast

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.
Posted
Regular

Posted
Regular

Code
Function FixDec(floatvalue As Float, decplaces As Integer) As Float
Return Floor(floatvalue*10^decplaces)/10^decplaces
EndAs usual, keep in mind Floating Point Mis-arithmetic!
Posted
Enthusiast

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.
Posted
Guru

gambafeliz said
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
I'd have just used string functions for something like this
Posted
Guru

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?
Posted
Enthusiast

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.
Posted
Guru

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!
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
Posted
Regular

Uhmmm… perhaps the question should be put in Gambas "Mailing List".cogier said
The purpose of Int is to create an Integer. So how can it return a Float?
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

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.
Posted
Guru

thatbruce said
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. :?
1 guest and 0 members have just viewed this.


