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

x
?
Solved

How to format a numeric field in Crystal to print an integer or a decimal depending on the value.

Posted on 2013-06-19
8
Medium Priority
?
521 Views
Last Modified: 2013-06-20
What would be the appropriate way to format a numeric field so that it will not print any zeros to the right of the decimal point.

8.00 would print as 8
1.40 would print as 1.4
1.36 would print as 1.36
0
Comment
Question by:Joe Brown
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 39260843
Do you only need 0, 1, or 2 decimals?

mlmcc
0
 

Author Comment

by:Joe Brown
ID: 39260987
I need all 3 if the number goes out that far. So, If I set the field to have 2 decimals, and the value returned on the report is 8.00 - how can I get that to report as 8. How can I get 1.40 to print as 1.4?

I want it to drop any zeros to the right of the decimal point and remove the decimal and print as an integer if it is a whole number.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 39261297
You don't ned to go to 3 decimals ever? Say 1.125?

mlmcc
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LVL 35

Expert Comment

by:James0628
ID: 39261981
Assuming that you never want to see more than 2 decimal places, you could create a formula similar to the following and put it on the report:

if Int ({field}) = {field} then
  CStr ({field}, 0)
else
  if ({field} * 100) \ 10 = {field} * 10 then
    CStr ({field}, 1)
  else
    CStr ({field}, 2)


 That seems to work.  It converts the number to a string, using different numbers of decimal places, based on the value.  FYI, the backwards slash (\) is integer division in CR.

 One basic limitation with that approach is that you won't be able to do summaries on that report field, because it's a string, but if you need a summary, you can just put the actual field on the report too, but suppressed, and then summarize that.


 FWIW, there are several other ways that you could handle those decimals.  That's just one option.

 James
0
 
LVL 101

Accepted Solution

by:
mlmcc earned 1000 total points
ID: 39262431
You can also control it through the format

Right click the field
Click FORMAT FIELD
Click the NUMBER TAB
Click CUSTOMIZE
CLick the formula button to the right of DECIMALS

if Int ({field}) = {field} then
     0
else  if (Int({field} * 10) = {field} * 10 then
    1
  else
    2

mlmcc
0
 
LVL 35

Assisted Solution

by:James0628
James0628 earned 1000 total points
ID: 39262519
I think you've got an extra "(",  or left out a ")".

else  if Int({field} * 10) = {field} * 10 then


 James
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 39262541
You're right

if Int ({field}) = {field} then
     0
else  if  Int({field} * 10) = {field} * 10 then
    1
  else
    2
0
 

Author Comment

by:Joe Brown
ID: 39263104
Wonderful. This works. Thank you for the formula mlmcc and the correct James.
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

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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