Jump to content
bernhard

MathParser adds Intelligence to your Number Inputfields

Recommended Posts

Brand new module that I've built because I'm too lazy to always open the calculator when I have to do some calculations (like vat calculations etc):

GiXnx31.gif 

 

It adds some magic to FieldtypeDecimal, FieldtypeFloat and FieldtypeInteger fieldtypes. Of course you can define which fields you want to extend:

sx65CQZ.png

https://github.com/BernhardBaumrock/MathParser

Any ideas or contributions are welcome. What do you think about the name? What do you think about the styling?

  • Like 12
  • Thanks 1

Share this post


Link to post
Share on other sites

Nice one @bernhard,

A couple of thoughts:

  1. Always safer to deal with IDs rather than names. If I have a float field called 'price' and tomorrow I decide to change its name to 'unit_price', MathParser code would not find it (unless of course one is using the autoload feature, in which case it might still throw the warning about field does not exist). The superuser would be forced to change the config.
  2. I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this?
  3. We started on the premise that we are lazy 😀. So why not take lazy to its logical conclusion? Below, I present lazy max! I've changed the module config textarea inputs to ASM Selects. The descriptions are just dummy text. Only compatible fields (float, integer, decimal [do we have a Fieldtype like this?]) are listed for selection. I don't have a Gitlab account, so I've attached a zip file with the php files to this post.

Having said above. The module did not work for me (even before the changes I made). Shouldn't it be auto-triggering?

Demo

math_parser_asm_inputs.thumb.gif.9a49f00b8a9123037167ed40f6ad31e2.gif

MathParser-kongondo-edits.zip

  • Like 4

Share this post


Link to post
Share on other sites
6 hours ago, kongondo said:

I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this?

Maybe using the Fieldtype selector in the module autoload setting?? Though I guess that limits the compatibility minimum version.

type=FieldtypeInteger|FiledtypeDecimal

 

  • Like 1

Share this post


Link to post
Share on other sites
12 hours ago, kongondo said:

decimal [do we have a Fieldtype like this?])

Sure we do, not yet in the core though... 🙂 One can give a thumbs-up over here to support the request of adding it to the core: https://github.com/processwire/processwire-requests/issues/126

EDIT: ot here, but Padloper 2 should utilize FiledtypeDecimal, for example prices should be of this type. There are lots of currencies where a price like 100,000,000.00 is quite possible....

Edited by szabesz
EDIT added
  • Like 2

Share this post


Link to post
Share on other sites

Hi @kongondo thx for your improvements!

On 10/5/2018 at 10:36 PM, kongondo said:

I don't have a Gitlab account

Thx, I've moved the module to github. I'll do that for all my public modules. The workflow is now the same for me (it somehow didn't work before so I stayed with GitLab) and the pw modules directory does also only auto-update versions from github, not gitlab. I've merged your changes.

 

On 10/5/2018 at 10:36 PM, kongondo said:

Having said above. The module did not work for me (even before the changes I made)

You forgot to change one occurance of id/name change. Not sure if/why it really didn't work before the changes. Can't really believe that 😉 Please try version 0.0.2

https://github.com/BernhardBaumrock/MathParser/commit/3b09d501b86df06f4502f8415db9a74d13ac5d23#diff-c4d3faaaa59bb6d7e0d306c2c1312e13L125

  • Like 2

Share this post


Link to post
Share on other sites
11 minutes ago, bernhard said:

I've merged your changes.

Great! I forgot to clean up some copy-paste comments here 🙂

Share this post


Link to post
Share on other sites

Thx, also added a showIf for the exclude config field.

https://github.com/BernhardBaumrock/MathParser/commit/e0f0c27ae07a6d19efb5ba768ae9313babcc873e

On 10/5/2018 at 10:36 PM, kongondo said:

I've noticed assets (MathParser.js, MathParser.css and math.js) are loaded in all Page Edits, even in ones where it is not required or where there are no compatible fields. Maybe there's no way to get around this?

I'm using renderReady - so I'm not sure why fields are loaded?! Any hints? Any experience with this? Working on it 🙂 

Share this post


Link to post
Share on other sites

v0.0.6 adds support for runtime fields, that means you can now use mathparser in your processmodules as well just by specifying one property:

/** @var InputfieldDecimal $f */
$f = $this->modules->get('InputfieldDecimal');
$f->label = "foo";
$f->attr('id+name', "bar");
$f->mathParser = true;

 

  • Like 2

Share this post


Link to post
Share on other sites

I've two points. Does the math lib calculate using decimal math? If not is probably should, if not you should at least remove the parseFloat in your javascript.

parseFloat("0.100000000000000005")
// 0.1

 

Share this post


Link to post
Share on other sites

Hi @LostKobrakai

2 hours ago, LostKobrakai said:

Does the math lib calculate using decimal math?

I don't know 🙂 https://mathjs.org/

2 hours ago, LostKobrakai said:

If not is probably should, if not you should at least remove the parseFloat in your javascript.


parseFloat("0.100000000000000005")
// 0.1

Could you explain a little more verbose please why you think that parsefloat is a problem? For all my use-cases I can think of, this is exactly the output I want to get...

Share this post


Link to post
Share on other sites

On the reasons why floats are bad for money values:

https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency 
https://mathjs.org/docs/datatypes/numbers.html#roundoff-errors

Math.js seems to support something called "BigNumber" to do arbitrary precition math, which should always be use for monetary calculations. But math.js does seem to also detect issues in floating point math and give you a rounded value, so maybe it's fine without that for simple calculations, where automatic rounding doesn't result in errors in the result.

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Thx @LostKobrakai for pointing me to the right spot in the docs! I use math.format with a precicion of 14 now for all calculations.

v0.0.6:

QMSbDcD.png

v0.0.7:

NPoaZND.png

jj2MGAp.png

VXVJmY7.png

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...