Print % sign

I am using the line:

   con.Screen.print("Battery %.2f % full", (Brain.Battery.voltage()/Brain.Battery.capacity())*100); 

and I get:
Battery 37.69 full when I want Battery 37.69% full

I tried searching the forum, but could not find anything.
Please Help :slight_smile:
Thanks in advance

Try:

   con.Screen.print("Battery %.2f%% full", (Brain.Battery.voltage()/Brain.Battery.capacity())*100); 

% is an escape character, so if you want an actual % in the output you have to put 2 in a row.

13 Likes

Thank you. I will try this out on Tuesday, and tell you how it goes. :slight_smile:

I want to add a bit more onto this. This document is very useful:


Also, if you’re unsure about the “.2” is before f, it’s basically stating how many decimal places to provide after the period.
Source
If you want to know about what floats, doubles, etc. do and how they work, check out my tutorial video explaining what they mean here.

2 Likes

Hi, I recently tried this with this line of code

Brain.Screen.print(“Battery lvl: %d %%”, Brain.Battery.capacity(pct));

however the output gives: Battery lvl: 100
the percent sign is still missing. I have tried many variations of this including /% and others but nothing sems to work. Anyone have a solution?

use a backslash before the %, not a forward slash

1 Like

That doesn’t work either

yea, this is an unfortunate side effect of the string getting parsed through a sprintf like function more than once.
here are two ways that work, they may not work in the future if I ever address that bug.

   Brain.Screen.print("Battery lvl: %d %%%%", Brain.Battery.capacity(pct));
   Brain.Screen.newLine();
   Brain.Screen.print("Battery lvl: %d %s", Brain.Battery.capacity(pct), "%%");
3 Likes

this works well thank you!

Says the guy who’s tournament software can’t escape percent signs correctly

1 Like

Do you know of a particular place in TM that doesn’t handle percent signs correctly? If so, sharing that information so it can be fixed would be appreciated.

6 Likes

In the most recent version of TM, when it tries to print out the elimination brackets there are un-escaped percent signs in front of the team numbers.

2 Likes

Is there a reason why it has to be parsed twice?

Printf style ‘%’ format specifiers are already confusing for the novice programmers. It would be so much better if everything was kept as simple and as close to standard C behavior as possible. This way they could rely on multiple online tutorials covering legacy C programming.

PXL_20210206_224034345
From this past weekend.

2 Likes

No, it’s a bug I need to deal with, it’s just been low priority. It’s won’t get fixed in 1.0.13 (or the next VEXcode release as it may be an SDK issue) as we already have a release candidate, but perhaps the following one.

2 Likes

That’s on me. I was converting some really old python strings from the old style (using the % operator) and forgot to delete the %. It has nothing to do with escaping a percent sign.

Your post that I quoted above was appropriate and helpful. It would have been better if you led with that post instead of the unnecessary and incorrect snark in your first post. Please try to always be respectful when interacting with others whether it’s in person or on a public forum.

4 Likes

Huh, I guess we were wrong. Odd.

I’d say if it was an old established bug you hadn’t gotten around to solving. Snark like “says the guy who can’t do X” would be warranted.

But given the situation a “hey speaking of percent signs” with the picture would have been much more appropriate. Or a separate thread / bug report of course.

Tldr time and place for snark, not here

2 Likes

But making fun of DWAB is fun.

1 Like

but this is the vex forum

2 Likes