CFLib.org – Common Function Library Project

splitNumber(number)

Last updated August 26, 2012

author

Darwan Leonardo Sitepu

Version: 1 | Requires: CF8 | Library: MathLib

Description:
Takes an numeric value and returns a struct with keys integer and decimal, each holding the relevant part of the original value.

Return Values:
Returns a struct with keys integer and decimal, containing the relevant parts of the original number

Example:

<cfset result = splitNumber(254.999989)>
<cfoutput>
Integer Value = #result.integer#<br /><!--- 254 --->
Decimal Value = #result.decimal#<br /><!--- 999989 --->
</cfoutput>

Parameters:

Name Description Required
number A numeric value to split into integer/decimal parts Yes

Full UDF Source:

/**
 * Splits a numeric value into integer and decimal parts
 * version 0.1 by Darwan Leonardo Sitepu
 * version 1.0 by Adam Cameron. Renamed function, simplified logic, fixed a data truncation bug, returns a struct rather than a query now.
 * 
 * @param number      A numeric value to split into integer/decimal parts (Required)
 * @return Returns a struct with keys integer and decimal, containing the relevant parts of the original number 
 * @author Darwan Leonardo Sitepu (dlns2001@yahoo.com) 
 * @version 1, August 26, 2012 
 */
public struct function splitNumber(required numeric number){
    var result = {};
    result.integer = listFirst(number, ".");
    if (listLen(number, ".") > 1){
        result.decimal = listRest(number, ".");
    }else{
        result.decimal = 0;
    }
    return result;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson