printf number format for constant width lat/long in exiftool - perl

Apparently I am misunderstanding the printf man page. (Or else it's a bug in exiftool 10.55 and 10.77)
I am trying to get GPS coordinates from image files with exiftool. I would like to make them the same width and without unnecessary spaces.
The format string I tried, and one of the results:
-coordFormat "%03d°%02d′%0d%02.5f″"
042°37′280.00000″ N, 002°05′510.00000″ W
(I don't need five decimal places—I just put that in temporarily to see whether any of the cameras wer being dishonest about the precision.) The three unnecessary spaces can't be helped; they are outside the format string’s control, but I did get rid of others that were in the default.  The leading zero for latitude isn't needed, but it is there because longitude uses the same format string.  One problem is the bogus zero inserted between floor(seconds) and its decimal point. The other problem is the false fractional part.  The default format for that file is 42 deg 37' 28.39" N, 2 deg 5' 51.96" W
Someone's "cheat sheet" said that my second digit should be the total width, including the decimal point, so I changed the seconds to "%08.5f" but all that did was add another bogus zero in front of the decimal point, e.g., 510.00000→5100.00000 (width of ten, not eight!).
A few years ago, I did something similar, and got the correct results.  But I didn't bother to save the script "for future reference."
(Several other SO answers agree with that "cheat sheet.")

It looks like the issue is with the seconds field, for which you have the format specifier %0d%02.5f. I'm not sure what you intended, but there can be only one % for each value to be rendered
If you're formatting longitude then you are dealing with values between -180 and 180. If you want five decimal points then the total width will be
One character for the sign + or -
Three characters for the integer part
One character for the decimal point .
Five fractional digits
giving a total field width of ten. Your full specifier will be %0+10.5f, giving output between «-180.00000» and «+180.00000»
You may use a space flag instead of the +, as in %0 10.5f, which will use a space instead of a + to indicate a positive number, rendering 180 as « 180.00000». The leading zero is there so that zeroes are use to fill the full ten character field with
When dealing with latitude, you will need a total width one character smaller. %0+9.5f will result in a range of «-90.00000» to «+90.00000». Of course you may use the same format specifier as for longitude, which will produce «-090.00000» to «+090.00000». This way the latitude and longitude seconds will have the same number of characters

The %0d is throwing you off. That part of the template is consuming the "51.0" seconds component of the coordinate, leaving nothing for the %02.5d part of the template.
printf "%0d", 51 ===> "51"
printf "%02.5f"; ===> "0.00000"
printf "%0d%02.5f", 51 ===> "510.00000"
So lose the %0d.
The 2 in %02.5f also doesn't do you any good. The number before the decimal place is the minimum length of the field, and the number after the decimal place is the number of decimal places to use. Since 5 decimal places will be printed, the output will be at least 7 characters, and the 2 value will be ignored.

First number is the width, second number is the number of decimal points so what you have currently (%2.5f) appears to be backwards. %5.2f would give you a number that occupies 5 characters and has 2 decimal places. For a number as big as 510, you probably want to make it %6.2f

Related

"Round" 2530.30 to 2599 in Postgres

I need to replace numbers like 2530.30 with 2599 in PostgreSQL.
I tried using ROUND(2530.30)+0.99 but it only changes the numbers after the decimal point to 99. So it results in 2530.99, which I don't want.
I want to remove fractional digits and replace the last two decimal digits with 99. I know I can just use an integer, but my assignment at school says I need to do this.
There should no be negative numbers, the assignment says that I should have a product that is sold for, let's say, 3500.50 dollars, I then need to make this number go from 3500.50 to 3599. Not 3500.99.
Divide by 100, truncate, multiply by 100 again:
SELECT trunc(2530.30 / 100) * 100 + 99;
This replaces all numbers in the range [2500, 2600) with 2599.
Or, in more general terms, it replaces the last two decimal digits with 99 and discards fractional digits (which also transforms 0 or 12.50 to 99).
Negative numbers cannot occur, as you say, so ignored.

Remove commas and decimal places from number field

I am trying to add two zero place holders in front of a field without changing the actual values involved. The field is an order number that is being pulled from MOMs. So right now that fields' formula is {cms.ORDERNO}.
When I try '00'+{cms.ORDERNO} the field displays 001,254.00. How can I remove the decimals and comma so it displays 001254?
The usual trick is to pad with plenty of extra digits on the left and then only take the six you really want from the right. This would handle any order number ranging from 1 to 999999.
right("000000" + totext({cms.ORDERNO}, "0"), 6)
When you don't specify a format string, as you tried, it uses default settings which usually come from Windows. By the way, if I recall correctly cstr() and totext() are equivalent for the most part but totext() has more options.
You should also be able to specify "000000" as the format string to produce the left-padded zeroes. Sadly I don't have Crystal Reports installed or I'd check it out for you to be sure. If this is the case then you probably don't need a formula if you just want to use the formatting options for the field on the canvas. If you do use a formula it's still simple.
totext({cms.ORDERNO}, "000000")
You definitely want to use the Replace formula a few times for this. The formula below converts ORDERNO into string, removes any commas and trailing decimal places, then adds the two zeroes at the beginning:
`00` + REPLACE(REPLACE(CSTR({cms.ORDERNO}),".00",""),",","")
So for example, if cms.ORDERNO is 1,254.00 the output from this formula would be 001254
I know this is older, but better solutions exists and I ran across this same issue. ToText has what you need built right in.
"00" + ToText({cms.ORDERNO}, 0, "")
From the Crystal Documentation:
ToText (x, y, z)
x is a Number or Currency value to be converted into a text string; it
can be a whole or fractional value.
y is a whole number indicating the number of decimal places to carry
the value in x to (This argument is optional.).
z is a single character text string indicating the character to be
used to separate thousands in x. Default is the character specified in
your International or Regional settings control panel. (This argument
is optional.)

Number of decimal digits to show

How to change the number of decimal digits?
Changing the format Matlab can show only 4 (if short) or 15 (if long). But I want exactly 3 digits to show.
To elaborate on Hamataro's answer, you could also use roundn function to round to a specific decimal precision, e.g.: roundn(1.23456789,-3) will yield 1.235. However, Matlab will still display the result in either of the formats you have mentioned, i.e 1.2350 if format is set to short, and 1.235000000000000 if format is set for long.
Alternatively, if you use sprintf, you can use the %g formatting option to display only a set number of digits, regardless of where the decimal point is. sprintf('%0.3g',1.23456789) yields 1.23; sprintf('%0.3g',12.3456789) yields 12.3
You can either use sprintf or do *
var2 = round(var1*1000)/1000

Formatting a float to a certain length with Powershell

I am trying to round/ only print out floating numbers to a certain length no matter what the value is (I have a document that it needs to fit properly in). I am using powershell but I haven't found anything useful.
There can be a max of 6 digits if you include a decimal point.
Here is what i am looking for below.
17.12356 => 17.1236
189.12345 => 189.123
12345 => 12345.0
122909808 => 122909 ( I haven't though of a proper way to show this case, suggestions would be helpful)
The G format specifier will truncate your number to a specified number of significant digits, but without any forward zero padding of small numbers. The following code gives an example of the kind of thing you might get for various types of numbers and with 5 significant digits (6 chars wide allowing for the decimal point). Note that for very large or small numbers you get 'scientific' notation, which will obviously be wider than 6 chars. For these cases you would need to find the large numbers and move the 'E' at least 4 spaces to the left , but then you lose a lot of detail. Up to you what you want to sacrifice, really.
PS > #(12345.678,123.45678,12345678,12345,123.4, 0.00123456) |%{"{0:G6}" -f $_} | %{if ($_.Leng
th -le7) {$_} else {$_.Substring(0,7)} }
12345.7
123.457
1.23457
12345
123.4
0.00123

format floating points in matlab using fprintf function

Consider the following code:
A1 = [9.9, 9900];
A2 = [8.8, 7.7 ; ...
8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec, A1, A2)
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
I would like to know what does 4.2f or 8.3f mean in this case? Does it means how many digit we should use after .?
For instance by looking on code, it seems for me difficult to understand what they mean, while .2 or .3 appears a bit clear, first digit 4 and 8 became difficult to interpret, if it is related to mantissa and exponent, then why do we need it there?
Please help me to clarify such things
The first number indicates the total number of character spaces (including the delimiting .) the number will take up when printed. The second - as you pointed out - represents the number of decimals.
For example, if you print 1.2 with 8.3f you get three empty spaces before the number:
1.200
12345678 characters total
If you were to use 5.2f your output would be.
1.20
12345 characters total
The second line was added by me to illustrate the total number of characters (including white space). It is not part of the original output
Edit
In your example, using 8.3f for 1.2 wouldn't make much sense. However, if you wanted to write lots of column data to a file that could easily be read by another program, this might be more useful (because the format could be known). E.g. Consider two columns %8.3f%8.3f (note how you do not need a space between the floating point number formatter). This could give you an output like this:
1.200 34.564
8503.000 101.008
... and so on so forth. Here, the leading blank space helps. It will fail when you have numbers above 9999.999 in this case.
Edit 2
In Matlab, if you specify a number of total characters that is less than the number of digits you have before the decimal point (or none at all), it will just print the entire number. E.g. using %2.3f will give you
1.200
with no leading white spaces. If you only cared about the decimals printed, you could also use %.3f which again results in
1.200