G
G
Georgy Kotov2019-04-12 14:25:06
JavaScript
Georgy Kotov, 2019-04-12 14:25:06

How to bring units of measurement into a human-understandable form?

Let's say there are different values ​​0.000003 kg, 100500 gr, etc. In general, they are not very readable, I would like a ready-made elegant algorithm for converting what kind of 0.000003 kg = 3mg or vice versa 100500gr = 100.5kg Moreover, it would be nice for different values, using an input sorted array, for example:

var measure = [
  {k:"гр",v:1000},
  {k:"кг",v:1},
  {k:"т",v:0.001}
];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2019-04-12
@politon

My option
Now it will be tied to any unit

C
crazywu, 2019-04-12
@crazywu

If the scatter in dimensions is not very large, then everything can be reduced to one unit of measurement and converted to any other for output.
If the spread is significant, then you can create an object in which, in addition to the value, the prefix from the SI system will be stored. You can also add conversion functions from the current prefix to any other.
But we must not turn a blind eye to the accuracy of calculations. because in the case when we are talking about values ​​with a large capacity, the numbers will be stored in the form of exponential notation. And this means that you will have some limitation on the number of significant digits in the number (for js, if I'm not mistaken, 21 digits).
Alternatively, if the values ​​still go beyond 21 significant digits, you can use the notation of the number as a string and, accordingly, string arithmetic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question