Gamestudio Links
Zorro Links
Newest Posts
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
M1 Oversampling
by 11honza11. 04/20/24 20:57
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (rki), 405 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
[Solved] Displaying decimal variables without extra zeros. #455093
10/09/15 01:13
10/09/15 01:13
Joined: Nov 2014
Posts: 24
ME
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

Joined: Nov 2014
Posts: 24
ME
Greetings, my apologies if this is something I should have figured out from the material out there, but the manual didn't seem to cover it and google game me solutions that only seem to work for C or C+.

Does anybody know how I might display a variable, with decimal places, without having it appear like 37.600? I had been using the disp command, but it forces me to choose how many decimal places I want when setting up the display. I would like to have three when my variable is at a value of 45.555, for example, but if it then changes to a 45.500 I would prefer to not have those extra zeros kicking around. I would prefer to compact this into a single script, rather than having to alter the display every time the number of digits change.

I played around some with the display options that let you draw a string, but I have been unable to find any way to convert a variable into a string. If there is such a way to do this, I think that I would be able to work with it.

Thanks for your feedback!

Last edited by Hawthourne; 10/14/15 21:10.
Re: Displaying decimal variables without extra zeros. [Re: Hawthourne] #455103
10/09/15 18:07
10/09/15 18:07
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
'str_for_num' is the function you are looking for but it works same as digits display so you will need to edit the string each time the variable changes in order to remove mantissas last zeros.

Click to reveal..

Code:
STRING *str_for_num_custom ( STRING *_str, var _num )
{
	STRING *_str2 = str_for_num ( _str, _num );
	if ( !str_stri ( _str2, "." ) )
		return _str2;
	var _count = 0;
	var _pos = str_len ( _str2 );
	var _last = _pos - 2;
	for ( ; _pos>_last; _pos-=1 )
	{
		if ( str_getchr ( _str2, _pos ) != 48 )
			break;
		_count += 1;
	}
	str_trunc ( _str2, _count );
	return _str2;
}




Salud!

Re: Displaying decimal variables without extra zeros. [Re: Hawthourne] #455256
10/14/15 21:35
10/14/15 21:35
Joined: Nov 2014
Posts: 24
ME
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

Joined: Nov 2014
Posts: 24
ME
Thanks a bunch. Using your outline + the manual I was able to figure it out (with a few changes, but a similar argument). Here is a piece of code with annotations in case anybody else has issues with this. This piece of code is in my function main.

Click to reveal..
var l=0; // Set a variable which will tell the length of your answer.
var r=0; // Set a variable which will tell you how much to cut off your answer.
while (1) //Set a loop to create your answer and display every tick.
{
c = a / b; // If you are calculating your variable elsewhere, this line is not needed. I'm using / because it lets us try various decimals.
str_for_float(answer,c); //Turns the variable "c" into a string with six decimal places. In my case, the global string is named answer.
l = str_len (answer); //Sets the variable l to be the length of the string.
while(str_getchr(answer,l)==48) //This condition checks the term in the lth position in the string (in other words, the last) to see if it is a zero (48 is the corresponding ASCII character for zero).
{l-=1; // if the last term is a zero, we subtract one from l and run the check again, now seeing if the previous term was a zero.
r=r+1; // before we check again, we update r to let us know we must remove another digit from the right side.
}
str_trunc(answer,r); //Once the loop checking for zeros is done, we remove r many terms from the end of the "answer" script.
draw_text(answer, 300, 350, vector (160,160,255)); //Now, we display it.
r=0; //Make sure we reset r, so that we don't take away too many digits next time we check.
wait (1); //And wait a tick so our loop doesn't crash the program.
}

Last edited by Hawthourne; 10/14/15 21:36.
Re: Displaying decimal variables without extra zeros. [Re: Hawthourne] #455264
10/15/15 13:31
10/15/15 13:31
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
I am glad of being helpful laugh


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1