Home » SQL & PL/SQL » SQL & PL/SQL » similar plsql function to BigDecimal java function (pl sql oracle)
similar plsql function to BigDecimal java function [message #425527] Fri, 09 October 2009 07:49 Go to next message
mohan1760
Messages: 59
Registered: June 2008
Member
I have this java function
and my requirement is to write similar function in plsql

thanks in advance............




/**
* This Function rounds the number (stored as BigDecimal) upto the decimal digits present in roundUnit, according to roundingType. Then Returns the number as BigDecimal.
*
* @param val
* Number to be rounded (stored as BigDecimal)
* @param roundUnit
* Number will be rounded upto the decimal digits present in roundUnit ( Eg. 0.1,0.01,0.001,1,10,5,0.5,0.05,0.005 )
* @param roundingType
* roundingType can be UP, DOWN or NEAREST
*/
public static BigDecimal roundNumber(BigDecimal val, BigDecimal roundUnit, String roundingType)
{
roundUnit = roundUnit.setScale(3, RoundingMode.HALF_EVEN);

int scale = roundUnit.stripTrailingZeros().scale();

log("roundNumber :: roundUnit :" + roundUnit + " Scale : " + scale);

BigDecimal roundedVal = null;

if (!roundUnit.toString().contains("5"))
{
if (roundingType.equals(ROUNDING_UP))
{
roundedVal = val.setScale(scale, RoundingMode.UP);
}
else if (roundingType.equals(ROUNDING_DOWN))
{
roundedVal = val.setScale(scale, RoundingMode.DOWN);
}
else if (roundingType.equals(ROUNDING_NEAREST))
{
roundedVal = val.setScale(scale, RoundingMode.HALF_UP);
}

return roundedVal;
}
else
{
BigDecimal scaleFactor = null;

if (scale == 0)
{
scaleFactor = new BigDecimal(0.1);
}
else
{
scaleFactor = new BigDecimal(10).pow(Math.abs(scale - 1));
}

log("roundNumber : scaleFactor :" + scaleFactor);

roundedVal = val.multiply(scaleFactor);

log("roundNumber : roundedAmount :" + roundedVal);

int intValue = roundedVal.intValue();

BigDecimal fraction = roundedVal.subtract(new BigDecimal(intValue));

BigDecimal lowestRoundedAmout = null;

if (scale == 0)
{
lowestRoundedAmout = new BigDecimal(intValue).divide(scaleFactor, RoundingMode.HALF_UP);
}
else
{
lowestRoundedAmout = new BigDecimal(intValue).divide(scaleFactor);
}

log("roundNumber :: roundedAmount :" + roundedVal + " intValue :" + intValue + " lowestRoundedAmout :" + lowestRoundedAmout + " fraction : " + fraction);

BigDecimal roundLwrLmt = null;

BigDecimal roundUprLmt = null;

if (fraction.compareTo(new BigDecimal(0.5)) < 0)
{
log("Less than 0.5");

roundLwrLmt = lowestRoundedAmout;

roundUprLmt = lowestRoundedAmout.add(roundUnit);
}

else if (fraction.compareTo(new BigDecimal(0.5)) >= 0)
{
log("Greater than 0.5");

roundLwrLmt = lowestRoundedAmout.add(roundUnit);

roundUprLmt = lowestRoundedAmout.add(roundUnit.multiply(new BigDecimal(2)));
}

if (roundingType.equals(ROUNDING_UP))
{
roundedVal = roundUprLmt;
}
else if (roundingType.equals(ROUNDING_DOWN))
{
roundedVal = roundLwrLmt;
}
else if (roundingType.equals(ROUNDING_NEAREST))
{
BigDecimal avgLwrUprLmt = roundLwrLmt.add(roundUnit.divide(new BigDecimal(2)));

log("roundNumber :: roundLwrLmt :" + roundLwrLmt + " roundUprLmt :" + roundUprLmt + " avgLwrUprLmt :" + avgLwrUprLmt);

if (val.compareTo(avgLwrUprLmt) < 0)
{
roundedVal = roundLwrLmt;
}
else
{
roundedVal = roundUprLmt;
}
}

return roundedVal;
}
}
Re: similar plsql function to BigDecimal java function [message #425528 is a reply to message #425527] Fri, 09 October 2009 07:50 Go to previous messageGo to next message
ThomasG
Messages: 3186
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
What's wrong with using the Oracle ROUND / FLOOR / CEIL functions?

[Updated on: Fri, 09 October 2009 07:56]

Report message to a moderator

Re: similar plsql function to BigDecimal java function [message #425842 is a reply to message #425527] Mon, 12 October 2009 08:11 Go to previous message
Kevin Meade
Messages: 2098
Registered: December 1999
Location: Connecticut USA
Senior Member
ah.. isn't accounting wonderful!

Tell people on this post what your java routine does. I mean, in business terms.

Looks like an accounting function where you are doing a specific kind of rounding sometimes seen in banking? What is the name of the rounding method your are trying to emulate?

Kevin

[Updated on: Mon, 12 October 2009 08:13]

Report message to a moderator

Previous Topic: running procs concurrently
Next Topic: full outer join (merged)
Goto Forum:
  


Current Time: Fri Sep 30 09:00:13 CDT 2016

Total time taken to generate the page: 0.28788 seconds