[Solved] Displaying decimal variables without extra zeros.

Posted By: Hawthourne

[Solved] Displaying decimal variables without extra zeros. - 10/09/15 01:13

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!
Posted By: txesmi

Re: Displaying decimal variables without extra zeros. - 10/09/15 18:07

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!
Posted By: Hawthourne

Re: Displaying decimal variables without extra zeros. - 10/14/15 21:35

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.
}
Posted By: txesmi

Re: Displaying decimal variables without extra zeros. - 10/15/15 13:31

I am glad of being helpful laugh
© 2024 lite-C Forums