-
Notifications
You must be signed in to change notification settings - Fork 723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to create decimals - 0.25, 0.5, 0.75... #237
Comments
Hi @drawnword! So I made this (demo). I had an extra slot, so I added another custom key to toggle adding or subtracting the fractional value. CSS .ui-keyboard-inv span {
color: green;
font-weight: bold;
font-size: 1.2em;
}
.ui-keyboard-inv.subtract span {
color: red;
} Script $.keyboard.layouts.numpad = {
'default': [
'{1/4} {1/2} {3/4} {inv}',
'= ( ) {b}',
'{clear} / * -',
'7 8 9 +',
'4 5 6 {sign}',
'1 2 3 %',
'0 {dec} {a} {c}'
]
}
$.extend($.keyboard.keyaction, {
'1/4': function (kb) {
kb.$preview.val(function(i,v){
return (parseFloat(v) || 0) + (.25 * (kb.invert ? -1 : 1) );
});
},
'1/2': function (kb) {
kb.$preview.val(function(i,v){
return (parseFloat(v) || 0) + (.5 * (kb.invert ? -1 : 1) );
});
},
'3/4': function (kb) {
kb.$preview.val(function(i,v){
return (parseFloat(v) || 0) + (.75 * (kb.invert ? -1 : 1) );
});
},
'inv': function (kb) {
kb.invert = !kb.invert;
kb.$keyboard.find('.ui-keyboard-inv').toggleClass('subtract', kb.invert);
}
});
$('#keyboard')
.keyboard({
layout: 'numpad',
usePreview: false,
display: {
'1/4': '\u00BC',
'1/2': '\u00BD',
'3/4': '\u00BE',
'inv': '\u00B1:Add or Subtract fractions'
}
})
// activate the typing extension
.addTyping({
showTyping: true,
delay: 50
}); |
It works! Thank you very much! Exactly, what I was looking for! |
How about restricting the user from entering more than just once .50 or .25... so, that the keyboard would work like a calculator...? I tried the following... to detect, if a decimal already exists, it would not create the new decimal value... but it doesn't seem to work... any hints, what I am doing wrong? 'half': function (kb) {
var pattern = /^\d+(?:\.\d{2})?$/;
if (pattern.test(base.$preview.val())) {
kb.$preview.val(function (i, v) {
return (parseFloat(v) || 0) + (.50 * (kb.invert ? -1 : 1));
});
}
}, Another issue, I've been trying to deal with is that when it produces the number .50... it leaves the zero out. -Is there a way to keep 2 decimals in view? |
Ok, try this (demo): var changeValue = function(kb, name, val){
if (kb.$keyboard[0][name]) { return; }
kb.$keyboard[0][name] = true;
var result = ((parseFloat(kb.$preview.val()) || 0) +
(val * (kb.invert ? -1 : 1))).toFixed(2);
kb.$preview
.val(result)
.caret( kb.lastCaret.start, kb.lastCaret.end );
};
$.extend($.keyboard.keyaction, {
'1/4': function (kb) {
changeValue(kb, 'quarter', 0.25);
},
'1/2': function (kb) {
changeValue(kb, 'half', 0.5);
},
'3/4': function (kb) {
changeValue(kb, 'threequarter', 0.75);
},
'inv': function (kb) {
kb.invert = !kb.invert;
kb.$keyboard.find('.ui-keyboard-inv').toggleClass('subtract', kb.invert);
}
}); This version:
|
I'm guessing this issue has been resolved, so I'm going to close it. If you continue to have problems, please feel free to continue this discussion. |
How would I build up special buttons, which would carry a display: 1/4, 1/2, 3/4, and then result 0.25, 0.5, 0.75 added into existing figure when keypad buttons are pressed?
The text was updated successfully, but these errors were encountered: