Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do you get multiple enemies?
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mobin12




PostPosted: Fri Feb 17, 2012 9:29 am   Post subject: RE:How do you get multiple enemies?

what would the length be if i only wanted the digits before the decimal
example:
realstr (38.567549, '?')
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Fri Feb 17, 2012 12:41 pm   Post subject: RE:How do you get multiple enemies?

If your only interest is the whole digits. Floor the number. It will round down to the nearest integer.

intstr(floor(38.234))
Dreadnought




PostPosted: Fri Feb 17, 2012 1:00 pm   Post subject: Re: How do you get multiple enemies?

In this case, realstr cannot help you. You must turn to other options erealstr and frealstr. In the special case you mentioned where you want the whole part you can use 'round(num)' to round or 'num div 1' to avoid rounding (I use div in case of negative numbers).

frealstr can be used in this case since it deals with the fractional part. To avoid leading spaces in the string we choose a width of zero and the function will increase it to the correct value. For the fraction width we choose 0, this width will not be increased, so we effectively remove the decimal part of the number. Note that frealstr will still produce a decimal point in the string, but will round to the nearest integer.

Example :
Turing:
var pi1000 :string := frealstr(3141.59265359,0,0)
put pi1000 % prints "3142."
put pi1000(1..length(pi1000)-1) % removing the last character (the decimal point) we obtain "3142"

% If now you want to keep two digits after the decimal point, we ask for a fractional width of 2.
put frealstr (3141.59653589,0,2) % will print "3141.59" rounding to two digits after the decimal point.

% Note that in the special case that you want to round to an integer
put intstr(round(3141.59653589)) % I don't need intstr for put, but this way I am still getting a string
% Or if you don't want any rounding at all
put intstr(3141.59653589 div 1) % Since div rounds toward zero


You can also use erealstr, but then it will display in scientific notation and make things more complicated.
Turing:
put erealstr (3141.59265359,0,0,0) % prints 3e+3 which is not what you want.
put erealstr (3141.59265359,0,3,0) % "3.142e+3" is still in scientific notation, which you don't want (I assume)


So if all you want is the integer part, I sugget using instr (round( )) or intstr ( () div 1), and if you want some form of fraction, you can use frealstr. However, you can also multiply but some power of 10, round to an integer, divide by the same power of 10 and then take the realstr of that.

Example:
Turing:
% If I want two digits after the decimal point
put realstr (round(3141.592653589 * 100) / 100,0)
% Is equivalent to
put frealstr (3141.592653589,0,2)


Hope this helps.
mobin12




PostPosted: Fri Feb 17, 2012 4:34 pm   Post subject: RE:How do you get multiple enemies?

ok so i want to use a variable that gets percentage out of 100 and it is a real.
But i want to use realstr to change it to a string so i can use it in a Font.Draw.
Something i have tried is: (This is part of my game to show how much health you have.)
Turing:

var h : string := ""
var healthpercentage : real
healthpercentage := (health/215) * 100
h:=realstr(healthpercentage,0)
Font.Draw (h, 481, 530, font2, white)
Font.Draw ("%", 518, 527, font1, white)
Raknarg




PostPosted: Fri Feb 17, 2012 4:50 pm   Post subject: RE:How do you get multiple enemies?

ok, so do you only want to show the integer portion or two decimal points after or something?
mobin12




PostPosted: Fri Feb 17, 2012 5:53 pm   Post subject: RE:How do you get multiple enemies?

only the integer and no decimal points and also no
decimal (if possible)
Dreadnought




PostPosted: Sat Feb 18, 2012 3:34 am   Post subject: Re: How do you get multiple enemies?

So basically, what you want (I think) is to display only the whole value of a percentage. The easiest way in my opinion is to simply multiply the decimal value by 100, round then convert the integer to a string.
Turing:

Font.Draw(intstr(round(0.7852*100)) + "%", 200, 200, defFontId, black)
% This will draw "79%"


Alternately, you can use frealstr(realNumber, 0, 2) and remove the first two digits using (3..*).
mobin12




PostPosted: Sat Feb 18, 2012 7:28 pm   Post subject: Re: How do you get multiple enemies?

yes thank you very much
This fixed my problem
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 23 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: