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

syd1980
Level 1

Likes

0 likes

Total Posts

10 posts

0 solutions
View profile

syd1980
Level 1

Likes

0 likes

Total Posts

10 posts

0 solutions
View profile
syd1980
Level 1

14-09-2020

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;

#### Accepted Solutions (0)

Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

9 solutions
View profile

Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

9 solutions
View profile
Kosta_Prokopiu
Level 3

15-09-2020

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)[0];
}
toFixed(NumericField2.formattedValue,2)

No unpredictable rounding with this one...

Check if you find any values where this does not work...

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)

Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

9 solutions
View profile

Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

9 solutions
View profile
Kosta_Prokopiu
Level 3

14-09-2020

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;
}
}

Thanks for the reply Kosta, but I'm unable to get any result in the third field.

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

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

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...

🙂 which ones are not working?

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...

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...
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

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.

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

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.

MVP

Likes

395 likes

Total Posts

2,686 posts

491 solutions
View profile

MVP

Likes

395 likes

Total Posts

2,686 posts

491 solutions
``parseInt(temp1, 10);``