I have a surcharge text box that I want to perform calculations based on a subtotal field. There is a minimum 4.50 which is calculated when the subtotal is <112.5. When the subtotal is >=112.5 the calculation is subtotal *0.04. My problem is that I don't know how to program the field to show as empty when the subtotal is 0.
Here is my code.
{
var nSubtotal = this.getField("Subtotal").value;
if(nSubtotal = "0")event.value = "";
if( nSubtotal >= 112.5) event.value = nSubtotal * 0.04;
if( nSubtotal < 112.5) event.value = 4.50;
}
This can be simplified (and be made to work) with something like this:
if (this.getField("Subtotal").value > 0) {
event.value = util.printf("%.2f", Math.min(560, Math.max(4.50, this.getField("Subtotal").value * 0.04))) ;
} else {
event.value = "" ;
}
Note that in this case, the formatting of the result field is done in the calculation, and therefore, the result field does NOT need any format set; this also prevents the result field to show a value based on incomplete calculations.
Related
I'm trying to build some training app for functions for school. The problem is:
Everytime the randomly picked number is lower than 0, my function shows +-, because
I have a fixed format for my function.
EXAMPLE
I tried to use the NumberFormat of the Intl-Package, but then I can't use the int-values correctly. Is there a way to show a plus sign for positive numbers, while they are still usable to work with them?
Code so far:
int randomNumberMinMax(int min, int max){
int randomminmax = min + Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min + Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x+$b)²+ $c";
You could only show the plus when the number is positive like this for example
String task = "f(x) = $a(x${b >= 0 ? "+" : ""}$b)²${c >= 0 ? "+" : ""} $c";
Is there any easy way to limit the number of digits after the decimal point with an TMP_InputField set at decimal type ?
For exemple, if the user type 12.347, I want the text to stop at 12.34
Thank you !
You can register to an event onValidateInput, parse the string to a float, and reformat it with $"{value:0.##}.
EDIT: If anyone else is looking for the exact same thing as me, I succeeded by doing this little trick :
if (_hasComa)
{
string[] charAfterComa = _inputField.text.Split(",");
string strAfterComa = charAfterComa[1];
for (int i = 0; i < strAfterComa.Length; i++)
{
if (i >= 2)
{
int index = strAfterComa.LastIndexOf(strAfterComa[i]);
if (index >= 0)
strAfterComa = strAfterComa.Substring(0, index);
}
}
_inputField.text = charAfterComa[0] + ',' + strAfterComa;
}
You got an option in the text mesh pro input field for limiting the characters that players enter
I have a function that counts and hides the number of columns that does not fit the screen. I want to exclude a column when resizing and hiding the columns. Here is what I have.
let ctrOfColumns = this.gridOptionsValue.columnApi.getAllColumns();
this returns the columns that i have. I want to exclude a specific column which has a colId of 'toBeExcludedId' so that It won't be included in the hiding of columns algo.
Here is my algo in hiding of the columns
let gridWidthOfMyTable = $('#idOfMyGrid').outerWidth();
let columnsToBeShown = [];
let columnsToBeHidden = [];
let totalWidthOfColumn = 0;
for(let x = 0 ; x < ctrOfColumns.length; x ++){
const singleColumn = ctrOfColumns[x];
totalWidthOfColumn += singleColumn.getMinWidth();
if (totalWidthOfColumn > gridWidthOfMyTable) {
columnsToBeHidden.push(singleColumn);
} else {
columnsToBeShown.push(singleColumn);
}
}
this.gridOptionsValue.columnApi.setColumnsVisible(columnsToBeShown, true);
this.gridOptionsValue.columnApi.setColumnsVisible(columnsToBeHidden, false);
There is no need to loop through all the values in your array. You can just use chaining and apply a filter directly to getAllColumns(), like this:
let ctrOfColumns = this.gridOptionsValue
.columnApi
.getAllColumns()
.filter((column) => column.colId !== 'toBeExcludedId');
I am using the following function to add all of my line totals together. Each of the totals will be a decimal number such as 23.45 . When the user enters any qty into any of text input boxes it should fire the function, but all I'm getting at the moment in grandtotal is NaN, who's nan is it and why is she messing with my script?
By the way, each of the linetotals is a span, which is filled with the line total calculation once a quantity is entered into the qty text box.
So basically, user comes along, selects a price from the line one drop down, so lets say 20.00 , they then enter the quantity into the text box qty, let's say 2, jQuery then multiplies qty by priceeach and output into the span with the id linetotal1. What I want to do is add all of the linetotals together for a grand total, but lets say if they don't enter anything into line 2, linetotal2, 3, 4, 5 will them be empty.
<script>
$(document).ready(function () {
$('input').on('keyup', function () {
linetotal1 = $('#linetotal1').text(),
linetotal2 = $('#linetotal2').text(),
linetotal3 = $('#linetotal3').text(),
linetotal4 = $('#linetotal4').text(),
linetotal5 = $('#linetotal5').text(),
grandtotal = parseFloat(linetotal1) + parseFloat(linetotal2) + parseFloat(linetotal3) + parseFloat(linetotal4) + parseFloat(linetotal5);
$('#grandtotal').text(grandtotal);
}); });
</script>
You could just loop from 1-5, and have the value default to 0 if it's a blank string:
var rawValue, grandtotal = 0;
for(var i=1; i<6; i++)
{
rawValue = $.trim($('#linetotal' + i).text());
if(rawValue == '') rawValue = 0;
grandtotal += parseFloat(rawValue);
}
$('#grandtotal').text(grandtotal);
jsFiddle Demo
If you wanted to do something a bit fancier with jQuery you could select each of the spans based on the ID starting with linetotal. This would work if you added more spans, where as with the for loop, you'd have to update the count. It would be better if the spans all had a common class, which you could use to easily select them.
var rawValue, grandtotal = 0;
$('span[id^="linetotal"]').each(function(i, elem){
rawValue = $.trim($(this).text());
if(rawValue == '') rawValue = 0;
grandtotal += parseFloat(rawValue);
});
$('#grandtotal').text(grandtotal);
jsFiddle Demo
You should do this by loop, i have done this before for my client check if my code helps you
var disValue = new Array();
var this_size = document.getElementsByName("myVal").length;
sum = 0;
for (i=0; i<this_size; i++)
{
disValue[i] = document.getElementsByName("myVal")[i].value;
}
for (i=0; i<this_size; i++)
{
sum = parseFloat(sum)+ parseFloat(disValue[i]);
}
if (sum != 0) {
document.getElementById("disc_test").innerHTML="<?php echo $this->__('You are saving') ?> "+sum +" <?php echo $this->__('on this order') ?>!";
}
ignore php tags :)
// stampDuty percentages
float stamp1 = (invalue * 1 / PCENT);
float stamp2 = (invalue * 2 / PCENT); // #defined PCENT as 100
// stamp duty
NSNumber *stampN1 = [NSNumber numberWithFloat:stamp1];
NSNumber *stampN2 = [NSNumber numberWithFloat:stamp2];
// then goes to
// Stamp Duty invalue is entered in the textfield by user
if (invalue <= 125000.0f) {
NSLog(#"Equal to or under 175,000 1% Stamp Duty");
//stampN0
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#", [currencyStyle stringFromNumber:stampN1]]];
// need a common variable variable is picked up here
// the value that is calculated here is used with other totals to create a grand final total
}
if (invalue >= 125001.0f && invalue <= 250000.0f) {
NSLog(#"Greater than 125,000 and less than 250,000 StampDuty 1%%");
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#",[currencyStyle stringFromNumber:stampN2]]];
// need a common variable is picked up here
}
// returns with the appropriate value
//the value that is calculated here is used with other totals to create a grand final total.
// eg float Beven = (invalue + SaleP + stamp1 etc etc
If I use stamp1 in the above calculation it works fine. What I'm looking for is a common variable to enter into this calculation variable "string". There are other if statements.
Hope you can help
I suggest the variable name actualStampValue.
Also note, your code contains two errors:
Your first if checks that the number is <=125000, but the string it prints says 175000.
Your second if checks that the number is >=125001, which leaves out all the values in between 125000 and 125001. This check should be >125000.