| 262 | * k? {number|undefined} |
| 263 | */ |
| 264 | function stringify(x, id, n, k) { |
| 265 | var e, s, |
| 266 | Big = x.constructor, |
| 267 | z = !x.c[0]; |
| 268 | |
| 269 | if (n !== UNDEFINED) { |
| 270 | if (n !== ~~n || n < (id == 3) || n > MAX_DP) { |
| 271 | throw Error(id == 3 ? INVALID + 'precision' : INVALID_DP); |
| 272 | } |
| 273 | |
| 274 | x = new Big(x); |
| 275 | |
| 276 | // The index of the digit that may be rounded up. |
| 277 | n = k - x.e; |
| 278 | |
| 279 | // Round? |
| 280 | if (x.c.length > ++k) round(x, n, Big.RM); |
| 281 | |
| 282 | // toFixed: recalculate k as x.e may have changed if value rounded up. |
| 283 | if (id == 2) k = x.e + n + 1; |
| 284 | |
| 285 | // Append zeros? |
| 286 | for (; x.c.length < k;) x.c.push(0); |
| 287 | } |
| 288 | |
| 289 | e = x.e; |
| 290 | s = x.c.join(''); |
| 291 | n = s.length; |
| 292 | |
| 293 | // Exponential notation? |
| 294 | if (id != 2 && (id == 1 || id == 3 && k <= e || e <= Big.NE || e >= Big.PE)) { |
| 295 | s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e; |
| 296 | |
| 297 | // Normal notation. |
| 298 | } else if (e < 0) { |
| 299 | for (; ++e;) s = '0' + s; |
| 300 | s = '0.' + s; |
| 301 | } else if (e > 0) { |
| 302 | if (++e > n) for (e -= n; e--;) s += '0'; |
| 303 | else if (e < n) s = s.slice(0, e) + '.' + s.slice(e); |
| 304 | } else if (n > 1) { |
| 305 | s = s.charAt(0) + '.' + s.slice(1); |
| 306 | } |
| 307 | |
| 308 | return x.s < 0 && (!z || id == 4) ? '-' + s : s; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | // Prototype/instance methods |