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 7: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


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1860
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.2.2
OS: Windows 10 Pro

Re: printf and sprintf leading zeros/spaces

Postby Hooray » Tue Oct 17, 2017 6: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: 11124
Joined: Tue Mar 25, 2008 8:40 am

Re: printf and sprintf leading zeros/spaces

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

"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1860
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.2.2
OS: Windows 10 Pro


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest