LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2012, 10:41 AM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
sprintf: how force leading 0 on floating point result


I need the result of a FP calculation to always show as xx.xxx, e.g. 00.123, 01.468, 23.739 (the result is always < 60)

As I read man printf
Quote:
The flag characters
The character % is followed by zero or more of the following flags:
...
0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and
G conversions, the converted value is padded on the left with zeros rather than
blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision
is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored.
For other conversions, the behavior is undefined.
the following code should produce the desired result, but as you see, it does not. What am I missing?
Code:
~ $ awk '{ss=$1*60; sss=sprintf("%02.3f", ss); print "x" sss}'
.01
x0.600

Last edited by porphyry5; 09-27-2012 at 10:43 AM.
 
Old 09-27-2012, 11:12 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try this
Code:
$ awk '{ss=$1*60; sss=sprintf("%06.3f", ss); print "x" sss}'
0.01
x00.600
or this
Code:
$ printf "%06.3f\n" 0.6
00.600
From man 3 printf:
Quote:
The field width
An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the
converted value has fewer characters than the field width, it will be padded with spaces on the left
(or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may
write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next
argument, or in the m-th argument, respectively, which must be of type int. A negative field width is
taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field
width cause truncation of a field; if the result of a conversion is wider than the field width, the
field is expanded to contain the conversion result.
So 6 here is the minimum number of characters occupied by your number including padding, dot etc.

Last edited by firstfire; 09-27-2012 at 11:14 AM.
 
1 members found this post helpful.
Old 09-27-2012, 11:34 AM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Many thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating-Point Reentrancy Hazelrah Linux - Embedded & Single-board computer 0 08-02-2011 02:11 AM
floating-point in script mitsos Programming 6 08-31-2006 07:14 AM
Floating Point in kernel 2.4.28 lucs Slackware 1 10-19-2005 08:33 AM
Floating point exception hemk76 Linux - Software 1 05-06-2005 11:49 PM
using expr to prinbt floating point result. max_rsr Linux - General 1 03-14-2005 01:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration