I'm stuck in a javascript code, which gives me wrong result, can anyone help me please

Avatar

Avatar

syd1980

Avatar

syd1980

syd1980

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;
//app.alert("temp1: " + temp1);
//convert to int. to drop remaining decimals
temp2 = parseInt(temp1);
//app.alert("temp2: " + temp2);
// divide by 100 to shift back 2 places
temp3 = temp2 / 100;
//app.alert("temp3: " + temp3);

// return data to field
this.rawValue = temp3;

VAT Result.jpg

 

 

 

Accepted Solutions (0)

Answers (3)

Answers (3)

Avatar

Avatar

Kosta_Prokopiu

Avatar

Kosta_Prokopiu

Kosta_Prokopiu

15-09-2020

Here we try a completely different approach

The third field is a Text Field as the result is a manipulated string

Kosta_Prokopiu_0-1600206632435.png

Set the pattern of the second field as shown here

Kosta_Prokopiu_3-1600207280996.png

 

Kosta_Prokopiu_1-1600206707550.png

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

Kosta_Prokopiu_2-1600207232223.png

 

Avatar

Avatar

Kosta_Prokopiu

Avatar

Kosta_Prokopiu

Kosta_Prokopiu

14-09-2020

This seems to work - check it out

Kosta_Prokopiu_0-1600102255149.png

I guess 5% VAT?

Kosta_Prokopiu_1-1600102331012.pngKosta_Prokopiu_2-1600102375376.png

function truncate(number)
{
if (number > 999) {
return number > 0
? Math.floor(number)
: Math.ceil(number);
} else {
return number;
}
}

Kosta_Prokopiu_3-1600102542860.png

 

 

syd1980

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

I'm frustrated now, since two days I'm struggling with this. Please Please Help me.

syd1980_0-1600111252480.png

syd1980_1-1600111307249.png

syd1980_2-1600111368424.png

 

 

 

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

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

Kosta_Prokopiu_0-1600153836945.png

 

syd1980

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.

it is not working fine for this number. Kindly check this below image...

syd1980_0-1600157277445.png

 

Kosta_Prokopiu
🙂 which ones are not working?
syd1980

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

syd1980_0-1600159216880.png

 

syd1980_1-1600159227524.png

 

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

Yes, now it's working fine. but sorry the other things got disturbed with this...

syd1980_0-1600166060492.png

syd1980_1-1600170402149.png

 

 

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.

 

Kosta_Prokopiu
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
syd1980

Yes please, I'm already glad and thankful for you kind help.

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.

Avatar

Avatar

radzmar

MVP

Avatar

radzmar

MVP

radzmar
MVP

14-09-2020

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