If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Crystal Reports > Format Decimal field, hide decimal when 0

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-11-07, 15:56
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Format Decimal field, hide decimal when 0

Hi,

I have a decimal field with 4 decimal places. How do I format the field so that it suppresses the decimal places that are 0 and not display the dot if all 4 decimal places are 0. Basically trim the 0's from the right.

Examples:

123.1200 = 123.12
422.1000 = 422.1
531.0000 = 531
665.9876 = 665.9875

Thanks
Reply With Quote
  #2 (permalink)  
Old 12-11-07, 16:35
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
There is abutton called "Remove Decimal" on the formatting toolbar.
Looks like "0.00" with an arrow.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 12-11-07, 17:31
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
I only want to remove the decimal when it is zero. What you suggested just changes the Decimal Places value under Number Format. I'm pretty sure that the only way to achieve this is using a formula, but I don't know how to do it.
Reply With Quote
  #4 (permalink)  
Old 12-12-07, 08:34
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Try this
Code:
If Right(ToText({Customers.Total Sales}), 3) = ".00" Then
  ToText({Customers.Total Sales}, 0)
Else
  ToText({Customers.Total Sales}, 2)
It's not the best way of doing this at alll, but right now I can't think of anything better.

Convert to string - test to see if the 3 rightmost characters ar ".00" - if they are, convert result to text using 0 decimal places, otherwise use 2.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 12-12-07, 11:21
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
ok but that still doesn't do what I'm looking for. Please look at the examples I gave:

123.1200 = 123.12
422.1000 = 422.1
531.0000 = 531
665.9876 = 665.9875
Reply With Quote
  #6 (permalink)  
Old 12-12-07, 12:38
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
I managed to figure out the formula.

NumberVar decimals;
decimals := ToNumber(StrReverse(Split(ToText(CurrentFieldValue , 4), ".")[2]));

if decimals = 0 then
ToText(CurrentFieldValue, 0)
else
ToText(CurrentFieldValue, Length(ToText(decimals, 0, "")))
  • I convert the value to text with 4 decimal places, ToText() seems to automatically use 2 decimal places if you don't specify.
  • I split the text at the decimal and get the decimal value which is stored at index 2
  • The decimal string is reversed and converted to a number which removes any 0's that were at the front because of the reverse
  • The if checks if the number is 0 which means that the decimal string was "0000", else I get the length of the decimal string which I use when formatting the value to text
Reply With Quote
  #7 (permalink)  
Old 09-05-11, 07:15
holythirteen holythirteen is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
Lightbulb The Best and simple answer(Farzad)

just try the code below at format filed->Number->Customize-> Decimals Code Part:

Code:
if CurrentFieldValue=Int(CurrentFieldValue)  then
    0
else
    2
Note: The number '2' is decimal place of yours and you can replace it by your own.

Maxitem Software Corp.
Reply With Quote
  #8 (permalink)  
Old 02-02-12, 09:44
detrelas detrelas is offline
Registered User
 
Join Date: Feb 2012
Posts: 1
thanks holythirteen

holythirteen's solution works awesome !!!!! Thanks man
Reply With Quote
  #9 (permalink)  
Old 04-25-12, 09:17
DrSprocks DrSprocks is offline
Registered User
 
Join Date: Apr 2012
Posts: 1
Clumsy but works

In order to set the decimal places to show the significant places only

The logic:
Example: Suppose Price is 0.1023000
strreverse(totext({Price},7)) ==> 0003201.0
tonumber( 0003201.0 ) ==> 3201.0
Round(tonumber( 3201.0 )) ==> 3201
len(totext( 3201 ,0)) ==> 4 , so set decimal places to 4

The code:
If isnull({Price}) then 2
//else len(totext(Round(tonumber(strreverse(totext({Price },7,'')))),0))
Reply With Quote
  #10 (permalink)  
Old 04-25-12, 11:28
gcfp gcfp is offline
Registered User
 
Join Date: Apr 2012
Posts: 1
Chain of ifs to deal with decimal places

I cannot say this is a smart solution to the problem from my point of view, seems sillyk that Crystal forces you to implement such a nasty formula for something a format function should handle easily

This is the solution I am giving for a scenario where, at least, we always want to show the figure with a minimum of 2 decimal places and a max of 7. Put this in the Crystal report field - format - decimals formula

If {Price}=truncate({price},2) then 2 else
If {Price}=truncate({price},3) then 3 else
If {Price}=truncate({price},4) then 4 else
If {Price}=truncate({price},5) then 5 else
If {Price}=truncate({price},6) then 6 else 7

Some examples

3 will be shown as 3.00
3.1 will be shown as 3.10
3.11 will be shown as 3.11
3.111 will be shonw as 3.111
and so on.

if removing completely the decimal part of the figure (for example, price = 6) is required, then add the extra if
If {Price}=truncate({price},0) then 0 else

On which case 3 would be shown as 3

and if you want to show one decimal place too add the following

If {Price}=truncate({price},1) then 1 else

On which case 3.1 would be shown as 3.1

I would be very glad to hear there is a more simple solution for this problem!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On