Reducing the number of decimal places in a variable...

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
only1platinum
Newbie
Posts: 15
Joined: Thu Aug 30, 2012 2:42 pm
Location: United States
Contact:

Reducing the number of decimal places in a variable...

Post by only1platinum » Mon Oct 22, 2012 7:59 pm

I am trying to change a variable so that it only displays a maximum of 7 decimal places in a variable. as stands right now there are 13+ decimal places showing. I know I can count the number of digits in the string but if there are more or less numbers before the decimal places that will change how many it removes right?

If I delete everything after 10 digits for example...

10.987648915346 would become 10.9876489

but if the number were...

110.987648915346 then this would be the result 110.987648

Does this all make since?

Thanks in advance for the help.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Oct 22, 2012 8:37 pm

Code: Select all

Let>NumVal=10.987648915346
VBSTART
VBEND
VBEval>FormatNumber(%NumVal%,7),FormatResult
MDL>FormatResult

only1platinum
Newbie
Posts: 15
Joined: Thu Aug 30, 2012 2:42 pm
Location: United States
Contact:

Post by only1platinum » Tue Oct 23, 2012 7:44 pm

This worked! Thanks very much.

One question though, why is the code for doing the function not contained within the vbstart and vbend commands?

Sorry, Showing my ignorance and inexperience here.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Oct 23, 2012 9:00 pm

You don't actually need the vbstart and vbend blocks in the latest version.

The VBEval means "Evaluate this vbscript expression". Since it's a buit-in vbscript function there's nothing to declare inside the vbstart/vbend. You don't need those lines.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Wed Oct 24, 2012 11:08 pm

I included the vbstart and vbend blocks because I didn't know which version of macro scheduler only1 has. That way, if he had an older version VBEval would work. :wink:

only1platinum
Newbie
Posts: 15
Joined: Thu Aug 30, 2012 2:42 pm
Location: United States
Contact:

Post by only1platinum » Thu Oct 25, 2012 7:37 pm

Thanks for the help guys. When I first tried this it worked like a champ. However now I am trying to use it today and its giving me a "vbscript runtimes are not properly installed". I have tried un-installing macro scheduler and the restarting and reinstalling to no avail. I also tried installing from microsoft the sp6 VB runtimes again and still no luck. Any thoughts? I am trying to use some of these values in a dialog box and its chopping off the numbers. Below is a sample of my code. You will see the call near the bottom.

AddDialogHandler>Excelo,buttonCalculate,OnClick,CalcVal

SRT>CalcVal

GetDialogProperty>Excelo,jobSheets,Text,jobShts
GetDialogProperty>Excelo,sheetSize,Text,sheetSz
GetDialogProperty>Excelo,totalUnits,Text,ttlUnits
GetDialogProperty>Excelo,sheetCutSec,Text,sheetCutSec
GetDialogProperty>Excelo,sheetItems,Text,shtItems
GetDialogProperty>Excelo,sheetPunchedSec,Text,shtPunchedSec
GetDialogProperty>Excelo,sheetK,Text,shtK
GetDialogProperty>Excelo,sheetC,Text,shtC
GetDialogProperty>Excelo,sheetM,Text,shtM
GetDialogProperty>Excelo,sheetY,Text,shtY
GetDialogProperty>Excelo,selectedPrinter,Text,Prntr
GetDialogProperty>Excelo,totalFeet,Caption,ttlFeet
GetDialogProperty>Excelo,totalPunchTime,Caption,ttlPunchTime
GetDialogProperty>Excelo,scrapQuantity,Caption,scrapQTY
GetDialogProperty>Excelo,FPP,Caption,feetPP
GetDialogProperty>Excelo,PM,Caption,peicesPM


//Calculate Total Feet
Let>ttlFeet={(%sheetSz%/12)*%jobShts%}

//Calculate Total Pieces
Let>actPrinted=%shtItems%*%jobShts%

//Calculate Pieces / Min.
Let>ttlPPM={%actPrinted%/(%ttlFeet%*%Prntr%)}

//Calculate Ink Usages
Let>ttlInkK=shtK/ttlUnits
Let>ttlInkC=shtC/ttlUnits
Let>ttlInkM=shtM/ttlUnits
Let>ttlInkY=shtY/ttlUnits
Let>ttlInkUsed=shtK
Add>ttlInkUsed,shtC
Add>ttlInkUsed,shtM
Add>ttlInkUsed,shtY

//Calculate Total Punch Time
Let>shtPunchedSec={%shtPunchedSec%*(%jobShts%/60)}

//Calculate Scrap Quantity
Let>scrapQTY=%actPrinted%-%ttlUnits%

//Calculate Feet Per Peice
Let>feetPP=%ttlFeet%/%actPrinted%

//Calculate Total Punch Time
//Let>shtPunchedSec={%shtPunchedSec%*(%jobShts%/60)}

SetDialogProperty>Excelo,totalFeet,Caption,%ttlFeet%
SetDialogProperty>Excelo,actualPrinted,Caption,%actPrinted%
SetDialogProperty>Excelo,PPM,Caption,%ttlPPM%
SetDialogProperty>Excelo,unitK,Caption,%ttlInkK%
SetDialogProperty>Excelo,unitM,Caption,%ttlInkM%
SetDialogProperty>Excelo,unitC,Caption,%ttlInkC%
SetDialogProperty>Excelo,unitY,Caption,%ttlInkY%
SetDialogProperty>Excelo,totalInk,Caption,%ttlInkUsed%
SetDialogProperty>Excelo,totalPunchTime,Caption,%ttlPunchTime%
SetDialogProperty>Excelo,scrapQuantity,Caption,%scrapQTY%
SetDialogProperty>Excelo,FPP,Caption,%feetPP%
SetDialogProperty>Excelo,PM,Caption,%peicesPM%
SetDialogProperty>Excelo,totalPunchTime,Caption,%shtPunchedSec%
VBEval>FormatNumber(%shtPunchedSec%,7),theResult

END>CalcVal


Show>Excelo,Dia1


If>Dia1=1

MessageModal>Processed!

EndIf

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Thu Nov 01, 2012 1:47 pm

Here is a different approach (without VB), but in the 2nd example, you will see one of the sortcomings.

Code: Select all

Let>DecimalConverter=1000000
Let>NumVal=10.987648915346
Let>NumVal={Trunc(%NumVal%*%DecimalConverter%)}
Let>NumVal={%NumVal%/%DecimalConverter%}
MDL>NumVal

Let>NumVal2=4.1
Let>NumVal2={Trunc(%NumVal2%*%DecimalConverter%)}
Let>NumVal2={(%NumVal2%/%DecimalConverter%)}
MDL>NumVal2
Thanks,
Jerry

[email protected]

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Nov 01, 2012 2:06 pm

I've always done this using the round function. The round function might change the last digit of the resulting number but that is the result I've always wanted. For example if the resulting seven decimal place number ends in zero do you really want the last number to be a zero? If yes then the round function is not what you want.

Code: Select all

//Decimal places = number of zeros
//7 decimal places would mean var "places" has 7 zeros
Let>Places=10000000
Let>number=10.987648915346
Let>newnumber={(round(%number%*%places%))/%places%}
MDL>Newnumber

only1platinum
Newbie
Posts: 15
Joined: Thu Aug 30, 2012 2:42 pm
Location: United States
Contact:

Post by only1platinum » Thu Nov 01, 2012 2:33 pm

JRL! Thats exactly what I was trying to do yesterday with this code. I could not for the life of me figure out hot to calculate the math with the round function. I tried looking through the help file for how to use it but was not successful. Thanks for explaining it. I bet this will work exactly for what I am needing to do. Thanks for all your help guys! I will give it a try and let you know how it comes out.

Nick

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts