Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-24-2020, 01:35 PM   #1
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
printf formatting for doubles

Centos, c++
Please accomodate typos as I cannot copy from my computer to here and am quite subject to little errors.
This write statement typifies the writes to a file:

fprintf( multi_column_file, %03.3f,  %03.3f,  %03.3f, a, b, c);
All numbers are less than 100.0 but the columns are not lined up. %4d seems to work fine with integers, but this does not work with doubles.
What needs to be done differently?
Old 06-24-2020, 02:07 PM   #2
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
%3.3f means three-character-wide field, and yet three are digits after the decimal point. Including the decimal point itself you already have four characters which is more than the whole field width. No wonder the fields are not aligned. To accommodate 100.0 you'll need at least %5.1f. In other words
3(integer part)+1(decimal point)+1(fractional part)=5

Last edited by shruggy; 06-24-2020 at 02:23 PM.
1 members found this post helpful.
Old 06-24-2020, 02:22 PM   #3
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
shruggy is correct. I was thinking %3.4 meant three characters to the left of the decimal point and four to the right. I switched to %8.4 and all is good.
Thank you for your time.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
LXer: Red Hat Profit Doubles on Corporate Demand LXer Syndicated Linux News 0 03-29-2006 12:54 AM
ATI Mobility Radeon 9000 doubles game speed ichi Linux - Software 1 07-31-2005 06:27 AM
STL vector of 3 doubles? true_atlantis Programming 2 04-22-2004 10:13 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

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