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 / Methods to format string representation of integers and floats?

Author
Message
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 24th Dec 2017 13:42
Either I'm blind, the documentation is lacking or an easy way to pad integers with zeros or set decimals in a float when converting to string do not exist in AGK.

In other languages it's easy enough, in Go for instance:

myFloat := 1.23456
myString := fmt.Sprintf("%2.2f", myFloat)

this will format the float to the string value "01.23"

For the life of me, I could not find anything similar in AGK. In my current project I need to pad an integer with zeroes in front to always output 4 digits (0000 to 9999), and another to format a float to display only one decimal point. So had to hack together a couple of functions to do this for me. These do work, but surely such methods should be part of AppGameKit?!

...or maybe they are already, it's just me being blind...
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 24th Dec 2017 14:25
Set the number of decimal places in the str() command, I.e.
Str(1.23456,2)
Will give 1.23
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 24th Dec 2017 16:57 Edited at: 24th Dec 2017 16:57
Thanks smallg, that took care of the number of decimal places to display - and allowed me to get rid of my own hacky function to do it.

No ideas on easier way to pad zeroes in front of an integer to have it always displayed of a fixed length, eg "0123" or "0005"? My function to do it reads len(str(myInt)) and if less than 4 chars long, will pad with 1 to 3 zeroes.
puzzler2018
User Banned
Posted: 24th Dec 2017 17:00
does my function help



Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 24th Dec 2017 23:07
nahh,
just do this right("00000000"+str(number),8)
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 25th Dec 2017 09:45
Quote: "right("00000000"+str(number),8)"


Brilliant and simple Markus
13/0
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 25th Dec 2017 11:46
Thanks Markus. That little snipped reduced my padding routine from a hacky function of about a dozen lines, to a simple 2 liner

And I learned something new. Thanks again Markus and smallg!

Login to post a reply

Server time is: 2024-04-19 02:15:29
Your offset time is: 2024-04-19 02:15:29