Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How Can I Make My Calculated Field Appear in a Given Format, for Example, No Decimal Places?

Posted on 2011-09-09
20
Medium Priority
?
282 Views
Last Modified: 2012-05-12
One final refinement I'd like to make. I'd like this calculated field (I actually mulitplied it by 1,000,000 to get a PPM) to show without decimal places. I am going to make this a Make Table Query, so that Minitab can feed off of it, so I need the formatting to be part of the table that will be made.
0
Comment
Question by:Rex85
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +3
20 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36513263
Use the Round() or Int() functions.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36513270
Round allows you to specify the # of decimals.
0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 36513272
or cast:

cast (iif(Total_QNs = 0 OR Total_Shipped = 0,0,  Total_QNs/Total_Shipped)  as numeric(18,0))
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Dale Fye
ID: 36513275
just to be clear, CAST will not work within Access, but will if you are creating a pass through query to SQL Server.
0
 

Author Comment

by:Rex85
ID: 36513300
fyed:

I tried the Round, but got Syntax Errors and it wouldn't Save. This is what I tried.

 ROUND(iif(Total_QNs = 0 OR Total_Shipped = 0,0,  (Total_QNs/Total_Shipped)*1000000)0) AS PPM
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 800 total points
ID: 36513309
You forgot the comma separating your IIF() from the 2nd parameter of Round().  Try:

 ROUND(iif(Total_QNs = 0 OR Total_Shipped = 0,0,  (Total_QNs/Total_Shipped)*1000000), 0) AS PPM

Is this all in Access, or are you using SQL Server?
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 600 total points
ID: 36513319
You may need to explicitly convert the output of IIf to a number (IIF returns a string).

Give this a try, too:

FormatNumber(iif(Total_QNs = 0 OR Total_Shipped = 0,0,  (Total_QNs/Total_Shipped)*1000000),0)
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 36513343
FormatNumber?

mbizup, when did that come along?  I've been using Access since 1995, and don't think I've ever seen a reference to that function.

I love it when I learn something new.  And I like the ability to wrap negatives in ().
0
 

Author Comment

by:Rex85
ID: 36513378
Fyed: This is all Access

mbizup: Weirdly, the output of the calcualtion (PPM) came out as a number, but the numerator and denominator fields came out as strings
0
 

Author Comment

by:Rex85
ID: 36513431
Using Format Number as below...

FormatNumber(NZ(QPR_Interstuhl_QNs_tbl.Total_QNs,0),0) AS Total_QNs

I can control the number of decimal places, but the output is a string, not a number.
0
 
LVL 75
ID: 36513554
"IIF returns a string)"
?

?IsNumeric(iif (1=1,101,200))
True

mx
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36514427
Rex,

Have you tried Fyed's last post (http:#a36513319) ?

Fyed,

I'm not sure when it came around, but I've gotten a lot of use out of FormatNumber in reports, queries and forms.  I first ran across it trying to put that () formatting for negatives in a combo box's drop down list.  There are a few other neat related functions:

http://office.microsoft.com/en-us/access-help/formatnumber-function-HA001228842.aspx?CTT=3

The downside is that you may have to explicitly convert it to a number in some situations (it looks like Rex found one of those situations).

mx,

Is numeric does not necessarily mean a variable, field, etc is of a numeric data type, it simply means that something can be evaluated as a number.

http://office.microsoft.com/en-us/access-help/isnumeric-function-HA001228868.aspx

for example, s here is defined as a string, but isnumeric(s) is true, and I believe - not positive -  you *may* have trouble using s as a number in some situations without an explicit type conversion (we definitely see a few questions here where strings that would pass an isDate() evaluation need to be wrapped in CDate() to make things work).

    Dim s As String
    s = "              1.25"
    Debug.Print IsNumeric(s)
0
 
LVL 75
ID: 36514506
Yep ... forgot about that.  Look at that horrible example in the KB:

"This example uses the IsNumeric function to determine if a variable can be evaluated as a number."

IsNumeric() is nowhere to be found in the example, lol !

mx
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36514530
I had read the text, but not the example in that KB article.

That is really funny!
0
 
LVL 75
ID: 36514546
Maddening!  
Also funny is that ... Help for IIF does not indicate what ... it returns, other than 'one of two parts '

But ... try this:

?vartype (iif (1=1,101,200))
 2

2 = Integer

?vartype (iif (1=1,999999,200))
 3

3 = Long Integer

?vartype (iif (1=1,"mbizup",200))
 8

8 = String.

So apparently, it returns the appropriate Data Type ...?

mx
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 600 total points
ID: 36516016
If you wish to remove the decimals, use Int as that is native SQL:

IIf(Total_Shipped = 0, 0, Int(Total_QNs / Total_Shipped * 1000000)) AS PPM

/gustav
0
 

Author Comment

by:Rex85
ID: 36522744
I get the same response whether I use Fyed's ROUND, Mbizup's FORMAT NUMBER, or Cactus Data's Int.

That appears to solve my question. I am confused, however, on how to award points.

Any suggestions on that? I really appreciate ALL of your help, and I don't want to offend anyone.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 36522792
Split the points as you like - no big deal here - but please pay attention to the reduced formula of mine which, of course, could be used with Round as well.

Note that Int rounds down and is faster while the other functions perform 4/5 rounding (as far as I know), and that FormatNumber returns text which may not be what you need.

/gustav
0
 

Author Closing Comment

by:Rex85
ID: 36523355
Thank you ALL very much.

Rex
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 36524165
You are welcome!

/gustav
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question