 Level 1

0% to Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

## I'm stuck in a javascript code, which gives me wrong result, can anyone help me please  Level 2
Hello folks,

Below is the JavaScript code for three fields 1- SubTotal 2- Actual VAT 3- VAT (this is the VAT result as per our requirement), everything is working fine whenever I'm giving any amount in the subtotal field it gives me the correct result without rounding off with 2 decimals. except for two number 41 & 51, with these two numbers, I suppose to get "2.05" and "2.55" respectively but the result what I'm getting with the below code are "2.04" & "2.54", unable to understand why. Is something wrong with my code? Could you please help me in solving this?

temp1 = VATnew.rawValue * 100;
//convert to int. to drop remaining decimals
temp2 = parseInt(temp1);
// divide by 100 to shift back 2 places
temp3 = temp2 / 100;

// return data to field
this.rawValue = temp3; 18 Replies  Level 10

The parseInt() method has a second parameter called radix. Its used the define the number base, for decimals its 10.

You should use it to avoid that the input is converted into a wrong number by assuming the wrong base.

``parseInt(temp1, 10);``  Level 2  Level 4

This seems to work - check it out I guess 5% VAT?  function truncate(number)
{
if (number > 999) {
return number > 0
? Math.floor(number)
: Math.ceil(number);
} else {
return number;
}
}   Level 2

Thanks for the reply Kosta, but I'm unable to get any result in the third field.     Level 4
I just realized that the screenshot does not show the whole thing - you need an additional line  Level 4
I just realized that the screenshot does not show the whole thing - you need an additional line  Level 4
I just realized that the screenshot does not show the whole thing - you need an additional line  Level 4

This forum system is really weird....  again

I just realized that the screenshot does not show the whole thing - you need an additional line

truncate(NumericField2.rawValue*100)/100 at the end   Level 2

Great, now it is working fine but not for all number...

I tried will all the same number as you did and one number extra like 1502.

it is not working fine for this number. Kindly check this below image...   Level 4
🙂 which ones are not working?  Level 2

Thanks for your reply and help Kosta. Great, now it is working fine but not for all number... I tried will all the same number as you did and one number extra like 1502 and more. it is not working fine for these numbers. Kindly check this below image...    Level 2
I don't understand why the code is not working properly for these random number, I'm just fedup. SORRY Kosta bothering you a lot...  Level 4
no problem, this is a rounding issue for single digit after the decimal. Change the line with the truncate call t: truncate(NumericField2.rawValue*10000)/10000 and then it does not do this unwanted rounding  Level 2

Yes, now it's working fine. but sorry the other things got disturbed with this...  I actually want this VAT value without roundoff and only with 2 decimals, earlier code was giving the value without roundoff but few random numbers were having issues.

now with this new line, all numbers are working fine but the VAT result is round off.  Level 4
yes, that is because we multiply with a higher number - if I have some time tomorrow I will see if I can find a different solution  Level 2

Kindly help me to achieve this, I hope you know what I actually want.

let me explain it again.

I just want the 5% of VAT result in only 2 decimals and without round off

accurately for all numbers.  Level 4

Here we try a completely different approach

The third field is a Text Field as the result is a manipulated string Set the pattern of the second field as shown here  The calculate script

function toFixed(num, fixed) {
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
return num.toString().match(re);
}
toFixed(NumericField2.formattedValue,2)

No unpredictable rounding with this one...

Check if you find any values where this does not work...   Level 4
I just realize that I used formattedValue but the toFixed function also works with the rawValue as I convert it from numeric to string. toFixed(NumericField2.rawValue,2)  