FirstClown

firstclown at firstclown.us

Archive for April 25th, 2008

Java Tips & Tricks: Double Trouble

At work, we've got a lot of new Java developers starting and I thought it might be a good idea to start up a little newsletter that, each week, will give little tips that Java programmers need to know but aren't usually told in classes or books. This is the first.


Double Trouble

Working with decimal numbers can be more complicated than it might seem. Many a Java programmer has been bitten by using doubles when calculating currency or another decimal value that relies on precision.

The problem

Let's say that we need to calculate a customer's bill. They used 80000.01 kWHs (big bill!) and we charge a dollar per kWH for this customer. To calculate that in cents, we just do:

double amount = 80000.01 * 100.0;

The answer we expect from this is 8000001. What we actually get, however, is 8000000.999999999! Why is this? Because doubles are inaccurate with large numbers. They just can't handle the calculation. They are actually made to be fast, not accurate.

So what should we do?

The Java language has anticipated this and created an object that can handle decimal numbers accurately. That class is BigDecimal. Using BigDecimals, our previous example would look like this:

BigDecimal kWH = new BigDecimal("80000.01");
BigDecimal price = new BigDecimal("100.0");
BigDecimal amount = kWH.multiply(price);

More complicated but we get the right answer of 8000001.00. We create the BigDecimal with Strings so that we keep the numbers safe from being rounded. If we called this as new BigDecimal(80000.01);, the number would be translated as a double first and then a BigDecimal, and we'll be passing it through an inaccurate format before getting it into a BigDecimal. We should avoid using doubles at all when dealing with important decimal numbers.

Remember: BigDecimal, not double or Double!

Your Company is the People You Serve

As I've been working more on some of my projects recently, and especially thinking of branching some of them out into bigger things, I've been wondering, "What would owning a company mean to me?"

What's the Purpose?

I have a feeling that most people think that the purpose of a company is to make money. This seems backwards to me. A company's purpose should be to bring value to people. If you're not bringing value, no one will care and the company might as well not exist. In fact, it won't once people figure out you're not really there to help them.

The reason I bring this up is, shouldn't every decision made by a business be led by it's purpose? Shouldn't every decision, every single one, be made by asking, "How does this help people?"

Should you quit your job and work full time on the business? Yes, if it means you can spend more time on helping the people your trying to serve.

Should you give yourself a raise? Yes, if it allows you to focus more on providing value to your customers.

Should you stop offering a product? Yes, if it means that you can focus on products the offer more value.

The people who derive value from the company is the company. The only reason any company should exist is for that purpose; to bring value to people. Any decision that increases that value is the right one. Any decision that decreases that value is the wrong one. Without those people, you're nothing; with them, that's all you need.

There is no other reason to have a company than to give value to the people you serve. Many may not agree, but the more I think about this, the more it just makes sense.

FirstClown is powered by WordPress
Entries (RSS) and Comments (RSS).