Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiply number with decimals to get whole number

Status
Not open for further replies.

Eprice

Technical User
May 6, 2003
209
US
Hi,
I am very new with Javascript and can't seem to get the result I want in a field. I have one field (GU4454.Skidders.Rate.1)that gets the rate from three other fields and have formatted it to 2 decimals. Another field uses that amount to get the premium but it is using three decimals instead of two. I'm not sure what the correct sytax is and can't find the information in Adobe help. I tried floor and round but neither gives me the correct result. What I want is to take 250000 * 1.43/100 to get 3575 but it uses 1.425 and gives me 3562. Here is what I have in the premium field:

var values = this.getField("GU4454.Skidders.Values.1")
var rate= this.getField("GU4454.Skidders.Rate.1")

var vNum = (values.value * rate.value)/100
var vNum1 = (values.value *1) /100


if((values.value !=0) && (rate.value ==0))
event.value = Math.round(vNum1)

if ((values.value !=0) && (rate.value !=0))
event.value = Math.round(vNum)

This is what is in the GU4454.Skidders.Rate.1 field:

var values = this.getField("GU4454.Skidders.Values.1")
var dedrate= this.getField("GU4454.Skidders.DedRate.1")
var load = this.getField("GU4454.Skidders.Load.1")
var mod= this.getField("GU4454.Skidders.Mod.1")

if (values.value !=0)
Math.floor(event.value = dedrate.value * load.value * mod.value)

if (event.value == 0)
event.value = ""

Any help with the correct syntax or where I can find help with Javascript calculations would be great. Thanks
Lisa
 
Dan,
Like I said, I am very, very new at Javascript, I usually do Visual Basic. I'm not sure where to use this and I get
rate has no properties error when I tried to use it with setting the variable.

var rate = this.getField("GU4454.Skidders.Rate.1.toFixed(2)")

Where and how do I use toFixed and thanks for trying to help a Javascript dummy.
Lisa
 
Eprice the syntax you show looks like Adobe PDF Javascript, so the function would be more like :

var rate = this.getField("GU4454.Skidders.Rate.1").toFixed(2);

you may also want to check to ensure that the data entered is numerical.
 
ggriffit,
Sorry, I should have said this was Adobe. I tried that and got the following error:

TypeError: this.getField("GU4454.Skidders.Rate.1").toFixed is not a function

So then I put it in a function and got no error but it still doesn't give me the correct answer. Here's what I have:
var values = this.getField("GU4454.Skidders.Values.1")
var rate = this.getField("GU4454.Skidders.Rate.1")

function ChangeRate(rate)
{
rate = rate.toFixed(2)
}

var vNum = (values.value * rate.value) /100
var vNum1 = (values.value *1) /100

if((values.value !=0) && (rate.value ==0))
event.value = Math.round(vNum1)

if ((values.value !=0) && (rate.value !=0))
event.value = Math.round(vNum)

 
There are several problems with that code:

- You're never calling the function
- The function doesn't return a value.

To fix the latter problem, change your function to be:

Code:
function ChangeRate(rate) {
   return(rate.toFixed(2));
}

Try that, and obviously call the function where you need to, assigning the return value, or whatever you want to do with it.

Anyway - if actually calling the function doesn't work, try this as an alternate function:

Code:
function ChangeRate(rate) {

	function roundNumber(floatNum, decPlaces){
		return Math.round(floatNum * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
	}

	return(roundNumber(rate, 2));
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
I am still working on it. I couldn't use the function for some reason I kept getting the rate.value is not a function error or something like that. I decided to make the rate field formatted correctly so the premium field would see the rounded up figure instead of the three decimal figure using this code in the GU4454.Skidders.Rate.1 field.

var S1Rvalues = this.getField("GU4454.Skidders.Values.1")
var S1Rdedrate = this.getField("GU4454.Skidders.DedRate.1")
var S1Rload = this.getField("GU4454.Skidders.Load.1")
var S1Rmod = this.getField("GU4454.Skidders.Mod.1")

if (S1Rvalues.value !=0)
var S1RNum = S1Rdedrate.value * S1Rload.value * S1Rmod.value
//IN EXCEL 1.425 WOULD BE 1.43 BUT IN THIS PROGRAM IT WOULD BE 1.42
//SO I HAD TO ROUND TO THREE DIGITS FIRST TO GET 1.425
var S1RNum1 = Math.round(S1RNum/0.001)*0.001
//THEN I USED MATH.CEIL TO MAKE IT ROUND UP FOR 2 DIGITS TO 1.43
var S1RNum2 = Math.ceil(S1RNum1/0.01)*0.01
event.value = (S1RNum2)

I was getting error "the value entered does not match the format of the field [GU4454.Skidders.Rate.1]" so I changed the format to none instead of number with 2 decimals and it works and gives me the correct result, but when I open the pdf the rate column shows NaN. If I ignore NaN and type in the figures it gives me the correct amount. I am back to work this morning and will try to get the NaN to go away.
As far as the function with toFixed() I asked a programmer for help as to where to call it and it still wouldn't work. Must have something to do with Adobe Acrobat maybe.
Thanks ahead of time for the help.
Lisa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top