@@ -284,7 +284,7 @@ function expressionToString(tokens, toJS) {
284
284
if ( nstack . length > 1 ) {
285
285
throw new Error ( 'invalid Expression (parity)' ) ;
286
286
}
287
- return nstack [ 0 ] ;
287
+ return String ( nstack [ 0 ] ) ;
288
288
}
289
289
290
290
function escapeValue ( v ) {
@@ -1308,6 +1308,33 @@ function condition(cond, yep, nope) {
1308
1308
return cond ? yep : nope ;
1309
1309
}
1310
1310
1311
+ /**
1312
+ * Decimal adjustment of a number.
1313
+ * From @escopecz.
1314
+ *
1315
+ * @param {Number } value The number.
1316
+ * @param {Integer } exp The exponent (the 10 logarithm of the adjustment base).
1317
+ * @return {Number } The adjusted value.
1318
+ */
1319
+ function roundTo ( value , exp ) {
1320
+ // If the exp is undefined or zero...
1321
+ if ( typeof exp === 'undefined' || + exp === 0 ) {
1322
+ return Math . round ( value ) ;
1323
+ }
1324
+ value = + value ;
1325
+ exp = - ( + exp ) ;
1326
+ // If the value is not a number or the exp is not an integer...
1327
+ if ( isNaN ( value ) || ! ( typeof exp === 'number' && exp % 1 === 0 ) ) {
1328
+ return NaN ;
1329
+ }
1330
+ // Shift
1331
+ value = value . toString ( ) . split ( 'e' ) ;
1332
+ value = Math . round ( + ( value [ 0 ] + 'e' + ( value [ 1 ] ? ( + value [ 1 ] - exp ) : - exp ) ) ) ;
1333
+ // Shift back
1334
+ value = value . toString ( ) . split ( 'e' ) ;
1335
+ return + ( value [ 0 ] + 'e' + ( value [ 1 ] ? ( + value [ 1 ] + exp ) : exp ) ) ;
1336
+ }
1337
+
1311
1338
function Parser ( options ) {
1312
1339
this . options = options || { } ;
1313
1340
this . unaryOps = {
@@ -1374,7 +1401,8 @@ function Parser(options) {
1374
1401
pow : Math . pow ,
1375
1402
atan2 : Math . atan2 ,
1376
1403
'if' : condition ,
1377
- gamma : gamma
1404
+ gamma : gamma ,
1405
+ roundTo : roundTo
1378
1406
} ;
1379
1407
1380
1408
this . consts = {
0 commit comments