Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How do I round a float?

Author
Message
Divided
FPSC Reloaded TGC Backer
11
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 29th Jun 2015 07:36
Greetings,

From all the research I have done, I cannot find evidence that rounding a float in agk exists. Could I be wrong?

I want to be able to display taps per second to two decimal places, however I seem to get results such as:

1.166667

even with using such rounding algorithms as:

( ( Taps / 30.0 ) * 100 ) / 100 )

I believe it is possible in other languages to round decimal places to display say:

1.17 ( double d = 1.166667;
DecimalFormat df = new DecimalFormat("#.##"); ) for example.

My question is simply; Is there a way to display only two decimal places or am I going to have to write IF statements questioning if the number is every possible two decimal float and print it as a String?

Cheers

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 29th Jun 2015 09:20
you mean this?
=Str( valueFloat, decimals )

AGK 108 (B)19 + AppGameKit (Steam) V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Divided
FPSC Reloaded TGC Backer
11
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 29th Jun 2015 09:47
YES!

You're a hero! That took me way to long to figure out. There is surprisingly little information on it out side of the command list in the documentation section.

I should probably have checked there first ay.



Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 29th Jun 2015 11:29
Quote: "There is surprisingly little information on it out side of the command list in the documentation section."


Where would you expect to find it?

AGK V2 user - Tier 1 (mostly)
Divided
FPSC Reloaded TGC Backer
11
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 29th Jun 2015 16:06
To be honest, I thought it would be a separate command...
I was looking in the maths section and looking up the declaration of floats and integers.

On the forum as well, I thought someone would be as blind as I am and I tried numerous google searches.

Guess I didn't expect it to be part of the str command.

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 29th Jun 2015 16:25
You are not rounding anything, you are just clipping the displayed value, therefore the string function. To do this in math, 1.166667 would become 1.17 instead of the clipped 1.16

It's like a scissors command for values

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 29th Jun 2015 16:42 Edited at: 29th Jun 2015 16:42
There is the Round command that can be used to actually round numbers. Okay, it only rounds to the nearest INT, but it can still be useful. For example, using Impetus73's example we could do the following:




The *100 and /100 simply shifts the decimal place along to round to the required accuracy.

V2 T1 (Mostly)
Uzmadesign
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Jun 2015 17:15
You can also round up or down (to integer) using floor() and ceil().

If you're dead set on rounding a float value you can do this:
ValFloat(str(myValue#, numDP))

Using AppGameKit V2 Tier 1
Divided
FPSC Reloaded TGC Backer
11
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 30th Jun 2015 06:28
Cheers lads!

Impetus73, that's what I meant to say haha

CJB, I seem to have forgotten "round" in my example

baxslash, very helpful, thank you!

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

Login to post a reply

Server time is: 2024-11-25 22:01:53
Your offset time is: 2024-11-25 22:01:53