jQueryCalculadora
jQueryCalculadora copied to clipboard
refactor number format using Intl.NumberFormat, add parent_element option
Two motivations:
-
Calculate/return numbers that can be directly cast as numbers without addition text parsing. The grouping ("thousands") separator is a problem. Using javascript Intl.NumberFormat class as an option allows very fine control of number to string formatting and provides additional options for currency and other locale differences. (as a minor benefit, it also simplifies the current logic that utilizes regex replacements that are somewhat inflexible)
-
Append to an optional parent_element instead of only $("body"). This allows the ticket to be visible when the input with calculadora is in a modal above the main body. In this case, for me, this creates a bug whereby the alignment is off (the ticket isn't close to the input to appear "attached") but at least it's visible.
I wrote this in a way that I believe is backward-compatible with the current options: decimals
and useCommaAsDecimalMark
. It could be further simplified to eliminate those options and only use this new numberFormat
option.
I did not create a minified map so that could be done if desired.
My previous pull-request addresses a bug-fix without any change to features or functionality; it would be unnecessary if merging this PR.
I know there haven't been commits to this project for many years. So this may just be a note of appreciation. I am submitting this to share my work in case it would be useful to you or others.
Cheers, Greg
https://github.com/Edddy/jQueryCalculadora/pull/6/commits/1159b85321d31074d1bd499b61060043ddce7102: calculator operations don't work when starting with 0. It also seems that the previous changes to '=' key disabled it. change to event key instead of code.