function ( val, precision ) { if ( precision == null ) { precision = 0; } if (…
- User
function ( val, precision ) { if ( precision == null ) { precision = 0; } if ( !precision ) { return Math.round( val ); } val *= Math.pow( 10, precision ); val += 0.5; val = Math.floor( val ); return val /= Math.pow( 10, precision ); };
- Assistant
This function takes in two parameters,
valandprecision. It first checks if theprecisionisnull, in which case it sets it to 0. Then, it checks ifprecisionis falsy (e.g. 0, "", null, false), in which case it rounds the value to the nearest integer usingMath.round()and returns the result. Otherwise, it multipliesvalby 10 raised to the power ofprecision, adds 0.5 to round it to the nearest decimal place, and then floors the value usingMath.floor(). Finally, it divides the value by 10 raised to the power ofprecisionand returns the result, which is the original value rounded to the specified precision.