Rounding to two decimal places without Round

Post

Posted
Rating:
#1 (In Topic #1052)
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 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. :)
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
My solution would be: -

Code (gambas)

  1. Print Int(2000.5354 * 100) / 100
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’

cogier said

Code (gambas)

  1. 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:

Code (gambas)

  1. Dim f As Float = 5.123456789
  2. Dim decimals As Byte = 6
  3.  
  4. Print Int(f * 10 ^ decimals) / 10 ^ decimals   ' or CInt(.....

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 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. :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

gambafeliz said

CInt conversion function without seeing your intention in the explanation or a code to understand you.
Simply…

Code (gambas)

  1. Print CInt(2000.5354 * 100) / 100

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
or perhaps, to generalize

Code

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!

Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘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 :)   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 ;)
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Guru
cogier is in the usergroup ‘Guru’
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?
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Guru
cogier is in the usergroup ‘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! :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
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
vuott is in the usergroup ‘Regular’

cogier said

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 !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
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.

Online now: No Back to the top

Post

Posted
Rating:
#15
Avatar
Guru
cogier is in the usergroup ‘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. :?
Online now: No Back to the top
1 guest and 0 members have just viewed this.