简体   繁体   中英

Table Format in C with Significant Figures

I am in the process of outputting some results I get from some arrays in C in a nice table format, but I want to cut the value down to 2 digits past the decimal point.

Part of my table code looks something like this (some fake arguments just to get the point across).

printf("%15d %15f %15f\n", arr[i], arr2[i], arr3[i])

I want to trim some of my double variables down so a 55.583159 will just return 55.58 without having to get rid of the double data type. So essentially I'm looking for a way to keep "%15f" and also add into it a "%.2f" in a nice clean manor.

Any suggestions?

you can combine the output format, so if you want 15 digit width ( %15f ) and 2 places after the decimal ( %.2f ) you can use ( %15.2f )

printf("%15d %15.2f %15.2f\n", arr[i], arr2[i], arr3[i])

this explains different formatting well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM