Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOUTPUT HAVING EXTRA INTEGERS 2

Status
Not open for further replies.

kingjjx

Programmer
Sep 18, 2001
181
0
0
US
Hi, In my database table where Im querying from, the administrator set the precision to 5. As a result, everytime I query from this table, It gives me at least five decimal points. Example. Instead of 123.25 it gives me 123.25000
Is there anyways in ColdFusion that I can cut the extra integers and set all outputs to have just 2 integers after the decimal point ?

thanks you
 
Do you want to truncate all the digits after two digits, or do you want to round the number to two decimal places?
 
<cfset n = 2> <!--- number of decimal place to keep --->
<cfset value = 123.25000>
<cfset value = Int(value * 10^n) / (10^n)>

Int() truncates. Use round(), ceiling(), or fix() for rounding. Fix() is similar to floor() in other languages.

 
So, if you are using CF5.0, you want to create a function which you can use all the time:

<cfscript>
function truncate(number, n) {
<!---
n is any interger (positive or negative).

Truncate a number (real or integer) to
n decimal places if n is > 0
n places to the left of the decimal point if n < 0 (zeros-out the places)

Behaves like Int() if n = 0
--->
return( int(number * 10^n)/(10^n) );
}
</cfscript>


 
hey , so does this mean i have to really specify the numbers that i want the the integers cut ?
like n=123.2500 , n=34.22000

im cfoutputing hundreds of records.
 
Yes, you obviously need to specify which numbers that you want truncated to two decimal places. However, 'n' is the number of decimal places. 'number' is the value that you want to modify.

given that &quot;number_field_name&quot; is a field in the query &quot;myquery&quot;,

<cfset n = 2>
<cfoutput query=&quot;myquery&quot;>
#truncate(myquery.number_field_name, n)#<br>
</cfoutput>
 
hey, i tried the above code .. its giving me the error:

An error occurred while evaluating the expression:

#truncate(myquery.sellingprice, n)#

Error near line 173, column 5.
------------------------------------------------------------
There is no ColdFusion function named truncate
 
hey, im using CF4.5 will this <cfscript> still work ?
 
You can only create custom functions in CF5, but there are several ColdFusion functions available to you in CF4.5:

1. Remove right 0 padding using Val():

=== START CODE EXAMPLE ===
<CFSET vNum1 = 123.25000>
<CFSET vNum2 = 123.25600>
<CFOUTPUT>
[COLOR=666666]<!--- 123.25 --->[/color]
#Val(VARIABLES.vNum1)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.256 --->[/color]
#Val(VARIABLES.vNum2)#[COLOR=000080]<BR>[/color]
</CFOUTPUT>
[COLOR=000080]<BR>[/color]
=== END CODE EXAMPLE ===

2. Round to the nearest 2 decimal places using DecimalFormat()

=== START CODE EXAMPLE ===
<CFSET vNum1 = 123.25000>
<CFSET vNum2 = 123.25600>
<CFSET vNum3 = 123>
<CFOUTPUT>
[COLOR=666666]<!--- 123.25 --->[/color]
#DecimalFormat(VARIABLES.vNum1)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.26 --->[/color]
#DecimalFormat(VARIABLES.vNum2)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.00 --->[/color]
#DecimalFormat(VARIABLES.vNum3)#[COLOR=000080]<BR>[/color]
</CFOUTPUT>
[COLOR=000080]<BR>[/color]
=== END CODE EXAMPLE ===

3. Round to custom decimal places using NumberFormat():

=== START CODE EXAMPLE ===
<CFSET vNum1 = 123.25600>
<CFOUTPUT>
[COLOR=666666]<!--- 124 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 124 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;99999999&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.3 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999.9&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.26 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999.99&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.256 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999.999&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.2560 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999.9999&quot;)#[COLOR=000080]<BR>[/color]
[COLOR=666666]<!--- 123.25600 --->[/color]
#NumberFormat(VARIABLES.vNum1, &quot;999.99999&quot;)#[COLOR=000080]<BR>[/color]
</CFOUTPUT>
[COLOR=000080]<BR>[/color]
=== END CODE EXAMPLE === - tleish
 
hey tleish, the problem is I am querying these numbers from a database and there's a lot of them so creating a CFSET would be too much task since there are hundreds of numbers.

Is there any other way to do it without using CFSET ?
thanks
 
There's no need for <CFSET> at all, I was just using it to set variables and show examples of what the number looked like before and after is is formated. If you want to use the functions with the query, just change the variable in the function

1. Remove right 0 padding using Val():

#Val(myquery.sellingprice)#

2. Round to the nearest 2 decimal places using DecimalFormat()

#DecimalFormat(myquery.sellingprice)#

3. Round to custom decimal places using NumberFormat():

#NumberFormat(myquery.sellingprice, &quot;999.99&quot;)#

- tleish
 
I don't believe you even need to use the Val and NumberFormat.

You requested that it automatically go to 2 decimal places.

DecimalPlace will format as a 2 decimal place number. This also includes rounding.
 
hey tleish it worked ! thanks for your help. thanks for your help too a440guy.

thanks !
 
I've posted all the information already. Are you actually reading it?

For CF4.5, you have to use the formula that I posted earlier. Here it is again:

<cfset n = 2> <!--- the number of decimal places --->
<cfoutput query=&quot;myquery&quot;>
#evaluate(Int(myquery.sellingprice * 10^n) / (10^n))#<BR>
</cfoutput>

You have to use evaluate() to simply output it as above. If you were assigning it to a variable, you wouldn't need that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top