Hi, first of all thanks for the great plugin. Is it possible to convert m to ft and in at the same time. So 1m will be 3ft and 3.37in? Thanks
You could do something like this:
a = 1m; [a to ft, a to in]
Or use an object:
a = 1m; {foot: a to ft, inch: a to in}
@handoyo, I think you mean converting 1m to feet, rounding toward zero, and then converting the remainder to inches? Math.js does not have a built-in way to do this, although it would definitely be useful in some cases. Time and degrees (minutes, seconds) could be handled the same way.
I would have suggested adding another signature to the to operator so that it accepts an array of units to "split" the unit into, but it looks like that syntax already does something else:
1 m to [feet, inch]
Current result:
[3.2808398950131 feet, 39.370078740157 inch]
Desired result:
[3 feet, 3.3700787401575 inch]
I'm not sure we want to redefine that behavior. If not, we could create a special function splitUnit instead:
splitUnit(1 m, [feet, inch])
[3 feet, 3.3700787401575 inch]
@FSMaxB Where should i place that code? Should i modify the js file first? Thanks.
@ericman314 Hi Eric, yes it is. I'm looking for that feature. It would be great if there is the splitUnit feature built in. Do you have any suggestion what should i do to get the desired result? Thanks.
OK, I think it's probably worth adding. @josdejong, @FSMaxB, any preference on which syntax to use?
Now I understand. Sorry, I'm only familiar with the metric system.
I can currently think of two possible options without substantially changing how units work.
inch and foot components.toString and toTex for units. But that would be more like a hack.I think the first one could be the way to go, but even then it might be a good idea to introduce unit systems or something like that to group units together.
Interesting idea Eric! I think a function like splitUnit is a nice idea. As far as I can think of, you would only use this to format a unit on screen for the user, in which case it may make more sense to extend the function math.format to allow entering a specific format for the unit, something like:
math.format(math.unit('1m'), { template: 'feet inch' }) // '3 feet 3.3700787401575 inch'
math.format(math.unit('1.2hour'), { template: 'hour minute second' }) // '1 hour 12 minute'
math.format(math.unit('1.2hour'), { template: 'hour:minute:second' }) // '01:12:00' how to do this?
We would have to give this template thing more thought, it isn't complete, it was just the first that popped in my mind.
Extending math.format with templates would be cool. I still like having a separate function splitUnits that we would call from inside math.format, and to provide users a way to do their own formatting or other logic with splitting units.
This might get ugly, but we could let the user use printf-style syntax for padding hours, minutes, and seconds. Or even use something like JavaScript's template literal syntax.
Anyhow, I will work on the splitUnit function while we think more about format templating.
Thanks a lot @ericman314 and @josdejong . I'll wait for the update.
JS's template literal syntax sounds good. We already use it for toString and toTex.
See #699 for the splitUnit function.
Sounds good Eric!
@ericman314 @josdejong I downloaded the latest version of MathJS.
When i try this code http://pastebin.com/f3ACU3Yn, i get the following error message in my console.

You're right, there is something wrong with the bundled files. I will fix this today, thanks for reporting.
Thanks a lot.
@handoyo I've fixed the bundles in v3.4.1 (just released). Sorry for the inconvenience.
@josdejong I just tested it. And it works correctly now. Thanks a lot for the great plugin. I want to ask another thing. How do i do the custom bundle? I don't quite understand on the how to. Are there any examples where i can learn from? Sorry if i use the same thread to ask about it. Thanks.
@handoyo you're welcome :) Yes, please open a new issue next time to keep discussions focused to one thing. There is documentation on how to do custom bundling here, does that help?
@josdejong Hi jos, sorry for late reply. I don't understand the steps on how to do custom bundling. Is that for nodejs? Or applicable to web based javascript too? Thanks.
To create a JavaScript bundle from multiple JavaScript files you have to use a command line tool like browserify, webpack, or rollup. The generated bundle can be loaded in the browser or in node.js.
@josdejong Thanks for the info. I will try to bundle it. Sorry for late reply.
use our calculator Meters to Feet conversion
@OKTOPUS-TN is this calculator powered by mathjs or is this totally unrelated shameless advertising?
I'm wondering if it's possible to use the splitUnit method with fractions. I'm working on a construction like calculator, so the idea is convert the inputs to multiples "easy to read" results, like:
(Update with units and conversion from m to ft)
(12 ft 1/4 in) + (12f t 1/4 in) = (3.6639500000000003 m) + (3.6639500000000003 m) = 7.3279000000000005 m === 24.041666666666667851 ft -----> Then *24ft 1/2 in*
As the result is in decimals, when I use the splitMethod it throws a decimal splitted result.
Thanks for your amazing work @josdejong , btw!
Thanks Hamlet!
Inputs like (12 ft 1/4 in) are not possible, those are interpreted as an implicit multiplication ((12 ft) * (1/4 in))
As for the output: I think you can write a very small wrapper named formatUnit or so around splitUnit, which tries to turn the numeric value of the last unit into a fraction and properly formats the output like you want.
You're right, to make that workaround I'm writing and interpreting those fractions by separated groups.
The wrapper method you mention is what I'm looking for. I'll try to build it and if it could be useful for the library I can share any achievement.
Thanks Jos!
馃憤
Most helpful comment
Extending
math.formatwith templates would be cool. I still like having a separate functionsplitUnitsthat we would call from insidemath.format, and to provide users a way to do their own formatting or other logic with splitting units.This might get ugly, but we could let the user use printf-style syntax for padding hours, minutes, and seconds. Or even use something like JavaScript's template literal syntax.
Anyhow, I will work on the
splitUnitfunction while we think more about format templating.