Script For Division Result Field Handling
Trying to print the weight on the label expressed in Kg and Lbs. I'm reading a text db file that has the weight of the box expressed in lbs in field11 of the db. The data in that field has 4 decimal positions. The Kg field that is printing is a simple VBscript: Field("items.Field 11")/ 2.2046 which convertes the lbs db field to a Kg value.
It works fine when the lbs value is 44.0920 because that equals an even 20Kg which is our main bag size but when we have 50 lb bags, the result is 22.67985122 Kg. I want the result field printed to only contain 2 decimal positions and rounded 22.68 Kg) I'd really like the lbs to print only 2 decimals too but haven't figured out how to do that.
I tried to add rounding to the result field but get a type mismatch error [string: tot_KG]
My script: (Field("items.Field 11")/ 2.2046) (Round("tot_KG"),2) "tot_KG" is a Share/Name I created for the sub-string that contains the calculation " Field("items.Field 11")/ 2.2046 " thinking it would be easier to reference that field name rather than a calculation and hoping it would contain the results of the calculation.
What vbscript lines do I need to put in to eliminate the mismatch error on the calculation field and get the result at 2 decimals positions? As you can see I know verry little VB scrip so help would be greatly appreciated.
It works fine when the lbs value is 44.0920 because that equals an even 20Kg which is our main bag size but when we have 50 lb bags, the result is 22.67985122 Kg. I want the result field printed to only contain 2 decimal positions and rounded 22.68 Kg) I'd really like the lbs to print only 2 decimals too but haven't figured out how to do that.
I tried to add rounding to the result field but get a type mismatch error [string: tot_KG]
My script: (Field("items.Field 11")/ 2.2046) (Round("tot_KG"),2) "tot_KG" is a Share/Name I created for the sub-string that contains the calculation " Field("items.Field 11")/ 2.2046 " thinking it would be easier to reference that field name rather than a calculation and hoping it would contain the results of the calculation.
What vbscript lines do I need to put in to eliminate the mismatch error on the calculation field and get the result at 2 decimals positions? As you can see I know verry little VB scrip so help would be greatly appreciated.
0
-
Legacy Poster
★ BarTender Hero ★
Use the FormatNumber() function.
For example, if my number is [b]1.123456[/b] and the share name is [i]data[/i], and I only want the first 2 decimal points, my line of code would be:
[code]formatnumber(data,2)[/code]
And that would return [b]1.12[/b]
For more information: www.w3schools.com/vbscript/func_formatnumber.asp0 -
Legacy Poster
★ BarTender Hero ★
I'm using a single-line Expression in Bartender 9.4 I tried your suggestion on the formatnumber by adding it to my original script: (Field("items.Field 11")/ 2.2046) (formatnumber("tot_KG",2))
Now I get the error saying "Type mismatch: formatnumber". The field tot_KG is the share/name I gave the calculated sub-string field. If I take the quotes away from the "tot_KG", I get a script error: Variable is undefined.
Using my field names and values how should that line read? I have a flavor for what the syntext should be but mosetly just try different combinations until the script errors go away. Do I need to do this with multi-line script or doesn't that matter?0 -
Legacy Poster
★ BarTender Hero ★
It looks like in your example, the correct code may in fact be: [code]formatnumber((Field("items.Field 11")/ 2.2046),2)[/code]
A single line script should work. Let me know what happens.0 -
Legacy Poster
★ BarTender Hero ★
It worked like a charm. Also used that to get the Lbs to show as 3 decimals. Thank you for bringing the formatnumber code to my attention and how to use it. 0 -
Legacy Poster
★ BarTender Hero ★
Thank you all for your help with my issue. One more question though, is there a way to code it so I just print the whole number if all the numbers to the right of the decimal are zeros? I can live with it the way it is but it would make the label cleaner looking if the result of the calc is 20.00 Kg it would just print 20 Kg and not have the .00 on the end. 0 -
Legacy Poster
★ BarTender Hero ★
Never mind I found the solution. If I just replace FormatNumber with Round, it handles the trailing decimals. Problem solved. 0 -
Legacy Poster
★ BarTender Hero ★
When I try to use FormatNumber or Round they both give me an error that I cannot use parenthesis in a Sub. 0 -
Michael Toupin (mtoupin
★ BarTender Hero ★
[quote name='JasonGriffith' timestamp='1344893828' post='3068']
When I try to use FormatNumber or Round they both give me an error that I cannot use parenthesis in a Sub.
[/quote]
Please post the entire string that you're trying to use. Sounds like just a malformed request.0 -
Legacy Poster
★ BarTender Hero ★
I'm not working any special math into it, I just need the this number which has 3 decimals in the database to be rounded to only 2 (and always 2, even when 0.00) on the label itself, but whether I use FormatNumber() or Round() I get the same error on both.
FormatNumber(Field("Charges.BoxGrossWtKG"),2)
Error 6900: "The following script error was found: <Line 1: FormatNumber(Field("Charges.BoxGrossWtKG"),2): Cannot use parentheses when calling a Sub>"0 -
Legacy Poster
★ BarTender Hero ★
I changed the field information to just Value instead and it seems to be working now. 0
Vous devez vous connecter pour laisser un commentaire.
Commentaires
10 commentaires