How can I round and print a floating point number in a minimum width format in Perl - perl

I wanted to do something like:
printf('%3.3f%% ', $percent);
But I'm still getting the output as:
99.999%
100.000%
I would like something like:
99.999%
100.000%
So it pads less than the full width with spaces. What format specifier would accomplish what I'm trying to do?

The number before the dot represents the minimum value for the full field width, so you need 7 in there to allow for three digits before and after the decimal point, and one more for the point itself:
printf "%7.3f%%\n", $_ for 99.999, 100;
output
99.999%
100.000%

Related

printf number format for constant width lat/long in exiftool

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

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

perl printf: float +1.2 should print as +001.200000

I have tried this:
printf("%+03.6f",+1.2);
but it gives me
+1.200000
what I need is
+001.200000
Any ideas?
The first number is the total number of characters, so you're asking it to provide minimum 6 digits of precision in a field of size 3 and it expands the width as needed to fit. Try 11 instead (width of your example output).