mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

global decimal limit / precision

Open cristeacosmin opened this issue 6 years ago • 3 comments

Hey,

Is there a way to set the global decimal limit / precision for all results ( not only BigNumbers ).

At the moment i am using your library to parse/evaluate a string with the results of a form to calculate some stuff, but i would like the results to be more "readable"

The full scenario: i have 5 standard form fields in the frontend (which will fill up the "scope") and i am calculating the distance like this :

calculated-distance=sum(hours-field hours to seconds, minutes-field minutes to seconds, seconds-field seconds to seconds)/sum(minutes-pace-field minutes to seconds, seconds-pace-field seconds to seconds)
calculated-distance-miles=calculated-distance km to miles
// taken from the user input
scope = { 
  'hours-fields' = 1,
  'minutes-field' = 56,
  'seconds-field' = 3,
  'minutes-pace-field' = 5,
  'seconds-pace-field' = 30,
}
//calculated values after user fills up the inputs using math.evaluate( formula, scope )
calculated-distance = 21.10 KM
calculated-distance-miles = 13.110932156207747 miles

and I would like it to be formatted like this 21.10 KM - 13.11 miles

Thanks for your patience 👍

cristeacosmin avatar May 04 '20 13:05 cristeacosmin

You can use a function like math.format to format any outputs in a standardized for with limited precision for example: https://mathjs.org/docs/reference/functions/format.html

josdejong avatar May 04 '20 18:05 josdejong

Hey thanks, got it working by formatting the result from the evaluation!

cristeacosmin avatar May 05 '20 10:05 cristeacosmin

👍

josdejong avatar May 05 '20 10:05 josdejong