README.md (1988B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # truncn 22 23 > Round a numeric value to the nearest multiple of 10^n toward zero. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var truncn = require( '@stdlib/math/base/special/truncn' ); 31 ``` 32 33 #### truncn( x, n ) 34 35 Rounds a `numeric` value to the nearest multiple of `10^n` toward zero. 36 37 ```javascript 38 // Round a value to 4 decimal places: 39 var v = truncn( 3.141592653589793, -4 ); 40 // returns 3.1415 41 42 // If n = 0, `truncn` behaves like `trunc`: 43 v = truncn( 3.141592653589793, 0 ); 44 // returns 3.0 45 46 // Round a value to the nearest thousand: 47 v = truncn( 12368.0, 3 ); 48 // returns 12000.0 49 ``` 50 51 </section> 52 53 <!-- /.usage --> 54 55 <section class="notes"> 56 57 ## Notes 58 59 - When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. 60 61 </section> 62 63 <!-- /.notes --> 64 65 <section class="examples"> 66 67 ## Examples 68 69 <!-- eslint no-undef: "error" --> 70 71 ```javascript 72 var randu = require( '@stdlib/random/base/randu' ); 73 var truncn = require( '@stdlib/math/base/special/truncn' ); 74 75 var x; 76 var n; 77 var v; 78 var i; 79 80 for ( i = 0; i < 100; i++ ) { 81 x = (randu()*100.0) - 50.0; 82 n = truncn( randu()*5.0, 0 ); 83 v = truncn( x, -n ); 84 console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v ); 85 } 86 ``` 87 88 </section> 89 90 <!-- /.examples --> 91 92 <section class="links"> 93 94 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 95 96 </section> 97 98 <!-- /.links -->