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