1.\" $NetBSD: strpct.3,v 1.3 2011/09/01 23:13:16 fair Exp $ 2.\" 3.\" Copyright (c) 2011 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This file was contributed to The NetBSD Foundation by Christos Zoulas. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27.\" POSSIBILITY OF SUCH DAMAGE. 28.\" 29.Dd September 1, 2011 30.Dt STRPCT 3 31.Os 32.Sh NAME 33.Nm strpct 34.Nd decimal percent formatter 35.Sh LIBRARY 36.Lb libutil 37.Sh SYNOPSIS 38.In util.h 39.Ft char * 40.Fn strpct "char *buf" "size_t bufsiz" "uintmax_t numerator" "uintmax_t denominator" "size_t precision" 41.Sh DESCRIPTION 42The 43.Fn strpct 44function formats the fraction represented by 45.Fa numerator 46and 47.Fa denominator 48into a percentage representation with given number of digits of 49.Fa precision 50without using floating point arithmetic. 51.Sh RETURN VALUES 52.Fn strpct 53always returns a pointer to a NUL-terminated formatted string which 54is placed in 55.Fa buf 56and is up to 57.Fa buflen 58characters. 59If there was an overflow, the formatted string will reflect that precision 60loss. 61.Sh EXAMPLES 62.Bd -literal -offset indent 63strpct(buf, buflen, 1, 16, 3); 64\(rA "6.250" 65strpct(buf, buflen, 1, 2, 0); 66\(rA "50" 67.Ed 68.Sh HISTORY 69.Fn strpct 70was originally implemented in 71.Xr csh 1 72for 73.Nx 1.3 . 74It printed into a static buffer, was not locale aware, handled 75.Ft unsigned long 76numbers, and printed a 77.Dq % 78at the end of the number. 79Other programs such as 80.Xr df 1 81and 82.Xr time 1 83started using it. 84.Fn strpct 85appeared separately in libutil for 86.Nx 6.0 . 87.Sh AUTHORS 88.An Erik E. Fair Aq fair@netbsd.org 89