code is all around

2 Jan


/**
* This is a VERY NICE double rounding function I found
* on the interwebs.
*
* @param valueToRound
* @param numberOfDecimalPlaces
* @return rounded value
*/
public static double round(double valueToRound, int numberOfDecimalPlaces)
{
double multipicationFactor = Math.pow(10, numberOfDecimalPlaces);
double interestedInZeroDPs = valueToRound * multipicationFactor;
return Math.round(interestedInZeroDPs) / multipicationFactor;
}

I don’t fully understand this just yet, but it works nicely, letting me limit the decimal places of doubles before going to println or printf or what-have-you. Something to throw into your own collection of personal methods and utilites.

Advertisements

Comments are welcome!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: