Board index FlightGear Development Nasal

printf and sprintf leading zeros/spaces

Nasal is the scripting language of FlightGear.

printf and sprintf leading zeros/spaces

Postby Necolatis » Mon Oct 16, 2017 8:00 pm

I have long been wishing for those methods to work like the C++ equivilant and be able to put in leading zeros or spaces.

It actually can do it, it just works a little different:

Code: Select all
printf("%.2f", 1.4);   = "1.40"
printf("%.1f", 1.4);   = "1.4"
printf("% 4.1f", 1.4); = " 1.4"
printf("%04.1f", 1.4); = "01.4"
printf("% 6.1f", 1.4); = "   1.4"
printf("%06.1f", 1.4); = "0001.4"


In other words the number includes the decimal point and the fraction part.

Just thought I would share.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: printf and sprintf leading zeros/spaces

Postby Hooray » Tue Oct 17, 2017 7:52 pm

Thanks for sharing, in general, it would be a good idea to add such things to the wiki - even if just as a newsletter addition in the form of "did you know".
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: printf and sprintf leading zeros/spaces

Postby Necolatis » Fri Oct 27, 2017 1:54 am

"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10


Return to Nasal

Who is online

Users browsing this forum: No registered users and 4 guests