Replies: 3 comments
-
That's an interesting idea. There is currently elegant no solution for this. You can use the function toStringHandler(node, options) {
if (node.isConstantNode) {
if (node.valueType === 'string') {
return '"' + node.value + '"';
}
else {
return math.format(math.eval(node.value), { precision: 5})
}
}
else {
return undefined;
}
}
var expr = 'x = 10.123456789';
var str = math.parse(expr).toString({ handler: toStringHandler });
console.log(str); // x = 10.123 Maybe we should introduce a new option to pass a var expr = 'x = 10.123456789';
var str = math.parse(expr).toString({
format: function (value) {
return math.format(value, { precision: 5});
}
});
console.log(str); // x = 10.123 and we could extend the existing function var expr = 'x = 10.123456789';
var str = math.format(math.parse(expr), { precision: 5});
console.log(str); // x = 10.123 |
Beta Was this translation helpful? Give feedback.
-
Thank you very much. The first option works like a charm. But I agree that including it into math.format would be perfect. Unfortunately I do not have the time to do it right now. |
Beta Was this translation helpful? Give feedback.
-
👍 let's keep this issue open as a feature request for this |
Beta Was this translation helpful? Give feedback.
-
Is there an elegant solution to format Nodes? Currently in lib/utils/string.js:90-100 (after
if (value && typeof value === 'object') {
) it's calling toString. But calling format would be nicer :D So every number in the Node is formatted.My naive solution would be to iterate through all properties of a node into depth and substitute ConstantNodes by their formatted pendant. Or to check for every Node type and act accordingly. But I guess this logic is already somewhere present?
Example of wished for behaviour:
math.format("x = 10.123456789", {decimals: 5}) returns "x = 10.12345"
Beta Was this translation helpful? Give feedback.
All reactions