Percentage Math in Javascript

Software, Web Development
Uncle Sam

Uncle Sam

We’ve all worked with the Math Objects while doing some kind of addition, subtraction, multiplication, and division to make our websites seem more alive.  Probably not everyone has done Tax math for purchases and refunds.  I’ve recently come across a problem that exposes a little hole in the Javascript world when dealing with percentage math against negative numbers. 

As most of you should be aware, dealing with percentages requires you take the percentage value and then divide that value by one-hundred (100) in order to perform currency math properly.  You must also try to round to two(2) decimal places as well because we don’t charge portions of a cent, only full cents of value.

The problem I’ve come across are the rare (update: not so rare afterall) instances when the negative rounded value for the value of tax is actually a different number than the same math performed on the positive value.  This can cause some pretty irate customers even though we’re talking about a penny’s worth of difference.  I know I would be upset if I saw they were doing bad math because I would be asking myself, “What other areas are they doing this bad math in?  My cellular minutes, my data usage by the megabyte, my water usage, etc.”  That could lead to bad publicity, like being published on the consumerist, and loss of customer confidence in your ability to provide quality service. 

When making a purchase at the local store, or even an online store, if you take a retail item and multiply it’s cost times the total percentage of tax to be included in the transaction you would expect it’s going to be same value if you do the same math with the negative value.  Such as when you needed to return that item to the store for whatever reason.  However, I’ll show you how to find where that may not be the case.  Noteworthy here is to realize we’re talking about a single tax value multiplied against a single currency value.  This may not come up if you were to do multiple tax calculations that should equal up to the same percentage you’re doing here for testing.  For Example: one calculation could be $10.50 against 5% gives you trouble but combining the math of $10.50 against a 3% and 2% calculations do not give you troublesome values. 

Seeing the problem in action

The script below is an example of what a developer might use for calculating taxes which would be a simple rounding after doing a percentage math problem.  However, if you use the value $10.50 and a percentage of 5%.  You’ll get $0.53 as a result, but for the -$10.50 at the same 5% you’ll get -$0.52. 

function testRounding1(RoundingAmount, PercentageAmount )
{
   //calculate and return tax value
   return Math.round(parseFloat(RoundingAmount*(PercentageAmount/100))*100)/100 ;
}

Using this script, you can try several values, both positive and negative to see how many combinations you can come up with that are part of the rounding problem.  I created a script you can use to help yourself find currency and percentage combinations, but beware, this script can crash your browser because of it taking so long.  I try to limit it to just about $10.00 limits at a time because of the amount of time it takes to show the user each “hit”. 

function findTroubleValues()
{
var NextElement = “”;
var intCount = 0;
//start at one cent and increment by one cent until $100.00
for (var dollarValue = 0.01; dollarValue < 100.01;)
{
//start at 0.1% and increment by 0.1% until 50.0%
for (var taxValue = 0.01; taxValue < 50.01;)
{
if ((testRounding1(dollarValue,taxValue)) != (testRounding1(dollarValue * -1,taxValue) * -1))
{
NextElement = “Purchase Amount: $” + dollarValue + ” has: $” + testRounding1(dollarValue,taxValue) + ” vs. $ ” + testRounding1(dollarValue * -1,taxValue) + ” @ ” + taxValue + “%
“;
//output to your test space the value of NextElement
intCount ++;
}
taxValue = Math.round((taxValue*100) + 1)/100;
//used instead of for loop’s taxValue+=0.01
}
dollarValue = Math.round((dollarValue*100) + 1)/100; //same as taxValue
}
alert( “Total HIT Count:” + intCount);
}

Wow! Now that’s a lot of “hits” here to show just how often this problem can rear it’s ugly head.  How can we deal with this problem?  Simple, do positive math all the time and remember to turn it back into negative before returning the value to the calling function.

Using the following script you’ll find that this handles that environment and you don’t have to worry about having to remember “oh yeah, if it’s negative use different test, etc.” in your day to day developing.¦lt;br />  

function computeTaxes(RoundingAmount, PercentageAmount)
{
   var blnNegCheck = new Boolean(false);
   //assume positive value
   var returnValue = 0.00;
   if(parseFloat(RoundingAmount) > 0 )
   {
      returnValue = RoundingAmount;
      blnNegCheck = false;
   }else{
      returnValue = RoundingAmount * -1; //initial value changed to positive
      blnNegCheck = true;
   }
   //initialize the percentage to the math equivelant
   var localPerc = parseFloat(PercentageAmount/100);
   //do the math
   returnValue = Math.round(parseFloat(RoundingAmount*localPerc)*100)/100;
   //return negative values back to negative
   if(blnNegCheck)
   {
      returnValue = returnValue * -1;
   }
return returnValue;
}

As you can see the first thing the code does is set a flag if the value passed is a negative value.  We then convert that negative value to positive, do the math, then return that value to negative before returning it to the calling function.  Performing the normal math without the conversions is done for positive values.  That way no matter what the value the same basic math and rounding is performed in a matching manner.

I have also generated a simple HTML file if you would like to see these items in action.  Email me if you can’t get the file to download.

I hope you’ve enjoyed this lesson and please feel free to give some constructive feedback as I enjoy providing quality information.

2 Comments

Web Server’s Variables

Diagnostics, Software Development, Web Development

Here’s another diagnostic tool for web developers: The Web Server’s Variables.  Constantly there are problems with communications or wondering what has been sent by the user’s website to the server and you sometimes wonder what variables might be useful for our programs.  However, I’ve never been too impressed with Google’s responses when looking for matches between Javascript and VBScript comparison between the two, or useful posts for that fact.  Anyway, before I start rambling about the lack of these kinds of posts, let’s get to the post.

As you may already know, I’m a big XML fan for building information blocks that can be used by my applications or communicating between applications.  So, I’ll be building XML blocks based on the variable name as the tags and the value inside.

VBScript – Server Variables

dim ResponseString
For Each ThisServerVar In Request.ServerVariables
       ResponseString = ResponseString + "<" + ThisServerVar + ">" + _
                                         Request.ServerVariables(ThisServerVar) + _
                                         "</" + ThisServerVar + ">"
Next

Javascript – Server Variables

var responseString = "";
for(var i=1; i<=Request.ServerVariables.Count(); i++)
{
   strKeyName = Request.ServerVariables.Key(i);
   strKeyValue = Request.ServerVariables.Item(strKeyName);
   responseString += "<" + strKeyName + ">" + strKeyValue + "</" + strKeyName + ">";
}

Now I can’t tell you where you will need to use this functionality.  But, it is a good diagnostic tool that can be very helpful from time to time when you’re just poking around trying to determine your communications.  Most of the time, you’ll know the exact server variable you’re looking to use.  But sometimes, when you’re programming in 6+ languages in a single week, you’re likely to forget the exact syntax and it’s nice to have something like this to reference.

Enjoy!

Please feel free to post any comments that others may find useful or even something I could use to update this entry.

1 Comment

iPhone Fix-It List

General Observation, Hardware, Software
My handy device needs more.

My handy iPhone needs more handy features

I like my iPhone and my wife likes her iPhone and we both have the 1st generation iPhone and don’t really miss the 3G features like the GPS much since we both have Navigation systems in our vehicles.  However, there are plenty of troubles that I’ve come across that just seem to be lost on the developers there in Apple Town.  Also, as a developer, I don’t want to blame everything on them as some lack of features could be a hardware architecture issue, manager’s choice, and/or “let’s hold onto that feature until version X” decisions.

1. Having to reboot

Why am I having to reboot my PHONE!? And every other day no less.  Was this built on the Windows 95 Operating System?  Are we working with Microsoft now, not Apple?  How many fan boys have I read that loved to point out that my Windows 95 machine had to be rebooted so often and here I am now having to reboot my phone.  Yes, I have a bunch of email accounts.  Yes, I have several applications that I’ve downloaded that are free.  But, this rebooting thing has been there since the beginning.

Signs you need to reboot your iPhone

  • Your Blue-tooth starts sounding like your battery is dying (static, etc.) but you just unhooked it fully charged this morning.
  • Your email isn’t checking at the intervals you’ve specified.
  • Your applications fail to load or load and crash even though they worked an hour ago.  (bless you Cuberunner for destroying my waiting room boredom!)
  • Your multi-touch actions start to delay beyond a few milliseconds.  Ugh, trying to turn that thing off when you’ve “swiped” to turn off and have to actually wait up to 10 seconds before the screen reacts is atrocious.

2. Playing songs ‘silently’!

If I can’t play the song and H-E-A-R it, then why play it?  Why give me three minutes of dead silence?  Why s it sync’d to my iPhone in the first place?  Yes, I know I can just skip to the next song when it happens, but good grief, I only have 8GB of space and 800MB of “silent” songs eats into that space pretty hard.  Also, having to fish out my iPhone just to skip past a “silent” song doesn’t seem like the next generation of computing to me (Yes Tech 5 influenced that line).  Here’s a few choices I could come up with for your next update:

  • Tell me the song can’t be played because of that dastardly DRM.
  • Skip the song completely and let me figure out that the song isn’t ever being played because of that dastardly DRM.
  • Don’t let me put DRM songs onto my iPhone, or any i<whatever> through iTunes, that won’t play on that device.  Now there’s an idea.

I’m sure there are a few other options.  That’s why I have a comments section below.

3.  Not checking email!

There are configuration settings for checking email for every account.  My primary email is supposed to be on “push” and I’ve set my check mail settings to “every 15 minutes”.  Too often I pick up my iPhone where the ‘last time email was checked’ value is over 5 hours ago!  I have a lot of email accounts and support a lot of customers through this thing as my primary source of emails to at least know that a problem has occured and I shouldn’t have to handle my iPhone and click on the swirl icon to force the iPhone to catch up on my emails.

4.  Not using Blue-tooth!

I’ve had too many phone calls where I hung up one call and then tapped my next incoming call button with my thumb only to not hear them.  You know why? Because I used my thumb to answer the phone call instead of having to drop my phone so I could tap the answer button on my Blue-tooth.  So my iPhone decided I didn’t want to use my Blue-tooth on my ear.  Why is that?  Why can’t, by default, the phone use the Blue-tooth and let me choose to disable it.  My wife’s van tries to take the phone call over no matter what, so why can’t any of the three Blue-tooth devices I’ve used on it seem to do the same for my iPhone?

5.  Blue-tooth button does what exactly?

I’ve tried to understand the single button on my phone when I’m not dropping my iPhone to answer using that thing.  It’s obvious when the phone rings that it answers the phone.  But what exactly is it supposed to do when I hold it down?

  • Does it call back the person I just talked with?
  • How about the last person I called?
  • Maybe the last person I called while connected to my Blue-tooth?
  • Could it be the last person that called me?
  • Perhaps it calls the next person I should be talking to because it integrated the Genius Feature and it has gone a little overboard with itself?

I certainly don’t know even after reading the directions because I’ve clicked that button and it started calling someone I hadn’t talked to in 3 days on that phone.  I personally liked it with my last phone where it would simply call back the last person I talked with on that phone indiscriminate of whether I called them or they called me first.  That way, if the call dropped I didn’t have to dig up my phone and find their number, just as I don’t have to dig up my phone to answer it.

Let me know what you think.  Comments are welcome.

2 Comments

The CC (Courtesy Copy) Field in Emails

General Observation
Bubba Teeth Pacifier

Has an excuse.

I’ve been plagued by people failing to understand the use of the CC or Courtesy Copy (sometimes called “Carbon Copy”) Field in emails since people realized they could put things into two different fields. I mean, this is a pretty simple concept to use this field. The Courtesy Copy is basically that: A Courtesy.

Setup:
John – Co-Worker #1
Arthur – Co-Worker #2

A Good Example

Your writing an email to John answering a question that he asked earlier in the day. You got part of that answer from Arthur and put it together with your knowledge of the question. As a courtesy, you put your answer to John together and put John’s email address in the TO field, of course, because that’s who you are sending the information. In addition, you CC Arthur in case you misunderstood what he explained to you. Also, it lets Arthur keep a copy of that email for future reference to that answer. But the main reason you used the CC field is because Arthur does NOT need to read it, it’s just information he contributed and he knows what he said. He could easily just copy this to his CC Folder in Outlook, or whatever email reader with rules ability he chose, and not need to read it ever. Who knows, you may need it again to explain it to someone else.

That’s a good way to use your CC Field. Now for a Bad Example.

A Bad Example

Your writing an email to John again, this time you’re talking about several items in a project that he and Arthur are assigned. You include action items that John needs to complete. However, during the writing you decide to mention in the email, “Arthur, Could you follow up with John on step number 13?”. You have already put Arthur in the CC field again because you want the whole team to know what’s going on with your other teammates.

Problem with Bad Example

Problem is, now that you’ve actually asked Arthur to do something, you should have moved his name into the TO field. I have received too many emails that were generated through a REPLY TO ALL action where I was in the CC Field only to be asked to do something. However, because I have a rule that automatically moves emails where “I am in the CC Field” to my “CC Folder” I may not see it. Also, I’m simply not going to lower myself to accommodate the ill educated. I think it’s far more effective if you do end up not reading something and have to tell that individual, “I was in the CC Field, so I don’t read Courtesy Copies until the weekend when I have time. Because that’s what it’s there for, not when you are talking directly to me. That’s what the TO Field is for.”

Conclusion:

Can’t we all just get along? Let’s use our email fields properly and our communications can be more effective for it!

2 Comments

Cell Phone Etiquette

General Observation

Have Cell phones led to our loss of person to person etiquette?

I enjoy reading from cnet.net for a variety of reasons. One of which is because they deal with the impacts to Society by technology. After reading Kent German’s Article on “Mind Your Cell phone manners” I had to put in my two cents worth.

Technology gives us a lot of conveniences but they can also impact us negatively against our fellow man. The use of the cell phone has, in my opinion, impacted our person to person etiquette in a disastrous way.

Giving our attention to one person at a time

Why is it that more and more people are trying to make an order at the local “whatever” store and continue to talk on a cell phone? I remember seeing movies where the really rich person was talking down to the working person and acting like they should be grateful for their presence alone. I was always offended by that type of attitude and my friends said they would never treat someone like that if they were rich. However, I see it constantly when they won’t give the simple courtesy of putting the person on their phone “on hold” long enough to give a clear request and wait on a polite response.

When you’re hanging out with your friends and talking about “nothing” while you’re perusing the local racks of clothes you would stop talking about whatever it is long enough to ask the clerk “How much is this?” and wait for a response before continuing your yapping, wouldn’t you? So why should it be different when you’re talking on your cell phone? It shouldn’t is the answer you’re looking for.

You have to give your attention to one person to give them the respect you would want given to you. How would you like it if the person you were talking too wouldn’t look your way or was having a second conversation at the same time? I know I can multitask and listen and type at the same time, but I don’t do it to give a personal interaction with that person. One exception I have is when I’m making notes of the conversation and at least the person I’m talking with is aware of that fact so they give me additional time during our conversation to allow me to take proper notes. I think this gives both people in the conversation an understood permission to divert some of my attention.

Body Language

Giving that personal attention gets you better service and vice verse. If you expect good service shouldn’t you give respect to the person you’re talking? Of course you should. In order to do that you need to get eye contact and “ear contact” so you both know you’re talking to each other. Your body language says a lot to the other person: raised eyebrows, tapping foot, huffing breath, or a smile. They all effect how someone responds to you. The body language you give off when you’re on the phone lets people know you’re not listening to anyone except the person on your phone: not making eye contact, talking to someone else, probably turning your body askew to the person behind the counter. All these physical signs make it hard for a person to be interested in what you’re asking them to perform.

Maybe that venti mocha chocalata half caff decaf with skim milk might end up with whole milk full caffeine with a shot of spit in your drink. Why not? You’re not paying attention to them why should they pay attention any more than you? While I certainly wouldn’t want a shot of saliva in my drink no matter how much of a tool someone might be it is only an example of what people are capable of when they deem themselves disrespected.

Use your indoor voice!

While I don’t advocate the use of phones inside an establishment for extended periods, I don’t think it’s realistic to stop usage altogether in a store like it appears is the case in Europe. However, I think that the most important piece of keeping things polite indoors is keeping your voice to a conversational level that you would maintain if that person was there in front of you. There are so many improvements in today’s phones that I know you can talk normally and the person on the other end can hear you just fine. I use a bluetooth earpiece and talk at a normal tone when I’m on the phone and have only had problems with people hearing me when people are talking around me that are facing my earpiece. My answer isn’t to get louder but to find a quieter location to complete my call.

We teach our children what’s right with our actions!

Yes, I went with the parents angle because I feel that parents are responsible for raising their children with proper manners. While adults are also committing the faux paxs of the cell phone usage, we must teach our children proper cell phone etiquette or not complain! What we show our children to be okay is what they take with them into the world as the example of what they consider right. If you don’t tell your children that talking on the phone while making an order is wrong, they won’t think it’s wrong. So you have to change your ways, too!

Here’s a list of simple etiquette that I think are very appropriate for cell phones. Certainly the list could be longer, so feel free to give some feedback.

Times when putting your phone on hold or hanging up to call them back later:

  • your turn at the counter to order food and/or drinks
  • approaching a sales person to ask questions
  • your turn at the check-outwhen at a social event!

Times when you should keep your phone call short:

  • When in the car with other people, and you shouldn’t be the one who started that phone call unless you’re car mates are involved.
  • At the restaurant. If it’s that important, take it somewhere else. You’re eating and no one wants to hear that!
  • When you’re with others. Aren’t the people your with right then deserving of your respect? If not, why are you with them in the first place, go hang with the person that called, they must be more important to you.

Times when your phone should be off or on vibrate:

  • At the movies. Get out of the theatre to answer that call if you feel you absolutely must.
  • During Church. Whether or not you’re bored to tears by the sermon, it’s a no-no!

Overall you want respsect and should give it. Remember the golden rule, “Treat those as you would like to be treated.”

No Comments