|
| 1 | +/* Copyright (C) 2015-2016 Bloomberg Finance L.P. |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 24 | + |
| 25 | +/*** |
| 26 | +Functions for interacting with JavaScript Number. |
| 27 | +See: [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number). |
| 28 | +*/ |
| 29 | + |
| 30 | +module Constants: { |
| 31 | + /** |
| 32 | + The smallest positive number represented in JavaScript. |
| 33 | + See [`Number.MIN_VALUE`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE) |
| 34 | + on MDN. |
| 35 | + |
| 36 | + ## Examples |
| 37 | + |
| 38 | + ```rescript |
| 39 | + Console.log(Int.Constants.minValue) |
| 40 | + ``` |
| 41 | + */ |
| 42 | + @inline |
| 43 | + let minValue: int |
| 44 | + /** |
| 45 | + The largest positive number represented in JavaScript. |
| 46 | + See [`Number.MAX_VALUE`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE) |
| 47 | + on MDN. |
| 48 | + |
| 49 | + ## Examples |
| 50 | + |
| 51 | + ```rescript |
| 52 | + Console.log(Int.Constants.maxValue) |
| 53 | + ``` |
| 54 | + */ |
| 55 | + @inline |
| 56 | + let maxValue: int |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | +`toExponential(n)` return a `string` representing the given value in exponential |
| 61 | +notation. |
| 62 | +See [`Number.toExponential`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential) |
| 63 | +on MDN. |
| 64 | + |
| 65 | +## Examples |
| 66 | + |
| 67 | +```rescript |
| 68 | +Int.toExponential(1000) // "1e+3" |
| 69 | +Int.toExponential(-1000) // "-1e+3" |
| 70 | +``` |
| 71 | +*/ |
| 72 | +@send |
| 73 | +external toExponential: int => string = "toExponential" |
| 74 | + |
| 75 | +/** |
| 76 | +`toExponential(n, ~digits)` return a `string` representing the given value in |
| 77 | +exponential notation. `digits` specifies how many digits should appear after |
| 78 | +the decimal point. See [`Number.toExponential`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential) |
| 79 | +on MDN. |
| 80 | + |
| 81 | +## Examples |
| 82 | + |
| 83 | +```rescript |
| 84 | +Int.toExponentialWithPrecision(77, ~digits=2) // "7.70e+1" |
| 85 | +Int.toExponentialWithPrecision(5678, ~digits=2) // "5.68e+3" |
| 86 | +``` |
| 87 | + |
| 88 | +## Exceptions |
| 89 | + |
| 90 | +- `RangeError`: If `digits` less than 0 or greater than 10. |
| 91 | +*/ |
| 92 | +@send |
| 93 | +external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential" |
| 94 | + |
| 95 | +/** |
| 96 | +`toFixed(n)` return a `string` representing the given value using fixed-point |
| 97 | +notation. See [`Number.toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) |
| 98 | +on MDN. |
| 99 | + |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +```rescript |
| 104 | +Int.toFixed(123456) // "123456.00" |
| 105 | +Int.toFixed(10) // "10.00" |
| 106 | +``` |
| 107 | +*/ |
| 108 | +@send |
| 109 | +external toFixed: int => string = "toFixed" |
| 110 | + |
| 111 | +/** |
| 112 | +`toFixedWithPrecision(n, ~digits)` return a `string` representing the given |
| 113 | +value using fixed-point notation. `digits` specifies how many digits should |
| 114 | +appear after the decimal point. See [`Number.toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) |
| 115 | +on MDN. |
| 116 | + |
| 117 | +## Examples |
| 118 | + |
| 119 | +```rescript |
| 120 | +Int.toFixed(300, ~digits=4) // "300.0000" |
| 121 | +Int.toFixed(300, ~digits=1) // "300.0" |
| 122 | +``` |
| 123 | + |
| 124 | +## Exceptions |
| 125 | + |
| 126 | +- `RangeError`: If `digits` is less than 0 or larger than 100. |
| 127 | +*/ |
| 128 | +@send |
| 129 | +external toFixedWithPrecision: (int, ~digits: int) => string = "toFixed" |
| 130 | + |
| 131 | +/** |
| 132 | +`toPrecision(n)` return a `string` representing the giver value with precision. |
| 133 | +This function omits the argument that controls precision, so it behaves like |
| 134 | +`toString`. See `toPrecisionWithPrecision` to control precision. See [`Number.toPrecision`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision) on MDN. |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +```rescript |
| 139 | +Int.toPrecision(100) // "100" |
| 140 | +Int.toPrecision(1) // "1" |
| 141 | +``` |
| 142 | +*/ |
| 143 | +@send |
| 144 | +external toPrecision: int => string = "toPrecision" |
| 145 | + |
| 146 | +/** |
| 147 | +`toPrecision(n, ~digits)` return a `string` representing the giver value with |
| 148 | +precision. `digits` specifies the number of significant digits. See [`Number.toPrecision`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision) on MDN. |
| 149 | + |
| 150 | +## Examples |
| 151 | + |
| 152 | +```rescript |
| 153 | +Int.toPrecision(100, ~digits=2) // "1.0e+2" |
| 154 | +Int.toPrecision(1) // "1.0" |
| 155 | +``` |
| 156 | + |
| 157 | +## Exceptions |
| 158 | + |
| 159 | +- `RangeError`: If `digits` is not between 1 and 100 (inclusive). |
| 160 | +Implementations are allowed to support larger and smaller values as well. |
| 161 | +ECMA-262 only requires a precision of up to 21 significant digits. |
| 162 | + |
| 163 | +*/ |
| 164 | +@send |
| 165 | +external toPrecisionWithPrecision: (int, ~digits: int) => string = "toPrecision" |
| 166 | + |
| 167 | +/** |
| 168 | +`toString(n)` return a `string` representing the given value. |
| 169 | +See [`Number.toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) |
| 170 | +on MDN. |
| 171 | + |
| 172 | +## Examples |
| 173 | + |
| 174 | +```rescript |
| 175 | +Int.toString(1000) // "1000" |
| 176 | +Int.toString(-1000) // "-1000" |
| 177 | +``` |
| 178 | +*/ |
| 179 | +@send |
| 180 | +external toString: int => string = "toString" |
| 181 | + |
| 182 | +/** |
| 183 | +`toStringWithRadix(n, ~radix)` return a `string` representing the given value. |
| 184 | +`~radix` specifies the radix base to use for the formatted number. |
| 185 | +See [`Number.toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) |
| 186 | +on MDN. |
| 187 | + |
| 188 | +## Examples |
| 189 | + |
| 190 | +```rescript |
| 191 | +Int.toString(6, ~radix=2) // "110" |
| 192 | +Int.toString(3735928559, ~radix=16) // "deadbeef" |
| 193 | +Int.toStringWithRadix(123456, ~radix=36) // "2n9c" |
| 194 | +``` |
| 195 | + |
| 196 | +## Exceptions |
| 197 | + |
| 198 | +`RangeError`: if `radix` is less than 2 or greater than 36. |
| 199 | +*/ |
| 200 | +@send |
| 201 | +external toStringWithRadix: (int, ~radix: int) => string = "toString" |
| 202 | + |
| 203 | +/** |
| 204 | +`toLocaleString(n)` return a `string` with language-sensitive representing the |
| 205 | +given value. See [`Number.toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) on MDN. |
| 206 | + |
| 207 | +## Examples |
| 208 | + |
| 209 | +```rescript |
| 210 | +// If the application uses English as the default language |
| 211 | +Int.toLocaleString(1000) // "1,000" |
| 212 | + |
| 213 | +// If the application uses Portuguese Brazil as the default language |
| 214 | +Int.toLocaleString(1000) // "1.000" |
| 215 | +``` |
| 216 | +*/ |
| 217 | +@send |
| 218 | +external toLocaleString: int => string = "toLocaleString" |
| 219 | + |
| 220 | +/** |
| 221 | +`toFloat(n)` return a `float` representing the given value. |
| 222 | + |
| 223 | +## Examples |
| 224 | + |
| 225 | +```rescript |
| 226 | +Int.toFloat(100) == 100.0 |
| 227 | +Int.toFloat(2) == 2.0 |
| 228 | +``` |
| 229 | +*/ |
| 230 | +external toFloat: int => float = "%identity" |
| 231 | + |
| 232 | +/** |
| 233 | +`fromFloat(n)` return an `int` representing the given value. The conversion is |
| 234 | +done by truncating the decimal part. |
| 235 | + |
| 236 | +## Examples |
| 237 | + |
| 238 | +```rescript |
| 239 | +Int.fromFloat(2.0) == 2 |
| 240 | +Int.fromFloat(1.999) == 1 |
| 241 | +Int.fromFloat(1.5) == 1 |
| 242 | +Int.fromFloat(0.9999) == 0 |
| 243 | +``` |
| 244 | +*/ |
| 245 | +external fromFloat: float => int = "%intoffloat" |
| 246 | + |
| 247 | +/** |
| 248 | +`fromString(~radix?, str)` return an `option<int>` representing the given value |
| 249 | +`str`. `~radix` specifies the radix base to use for the formatted number. |
| 250 | + |
| 251 | +## Examples |
| 252 | + |
| 253 | +```rescript |
| 254 | +Int.fromString("0") == Some(0) |
| 255 | +Int.fromString("NaN") == None |
| 256 | +Int.fromString(~radix=2, "6") == None |
| 257 | +``` |
| 258 | +*/ |
| 259 | +let fromString: (~radix: int=?, string) => option<int> |
| 260 | + |
| 261 | +/** |
| 262 | +`mod(n1, n2)` calculates the modulo (remainder after division) of two integers. |
| 263 | + |
| 264 | +## Examples |
| 265 | + |
| 266 | +```rescript |
| 267 | +Int.mod(7, 4) == 3 |
| 268 | +``` |
| 269 | +*/ |
| 270 | +external mod: (int, int) => int = "%modint" |
0 commit comments