algorithm
-
Calculate price excluding VAT
Sometimes you need to calculate the price of a product excluding VAT, and the only details you have is the amount including vat and the vat percent. This might be a bit tricky in some applications when there are mixed VAT percentages. For example, you paid 1000 space credits and in that sum there is…
-
Validate Swedish personnummer and organisationsnummer
JavaScript functions for validating Swedish personal identity numbers (personnummer), and organisation numbers (organisationsnummer). The functions for personal identity number will also validate co-ordination number (samordningsnummer).
-
(PHP) sorting an array on a specific value
Simple way to sort an array with usort on a specific value using a comparator. [php] usort($myArray, function ($a, $b) { return $a[‘foo’] > $b[‘foo’]; }); [/php]
-
(Android) Convert Density-independent Pixels to Pixels (dp 2 px)
A simple function to convert dp into pixels. Use when you have to pass pixels to a method and want to keep everything to scale. [java] private int dp2px(int dp) { float scale = getResources().getDisplayMetrics().density; int pixels = (int) (dp * scale + 0.5f); return pixels; } [/java] From Android Developer Center : pxPixels –…
-
(C#) Generate random readable passwords
Here’s a super simple function that generates humanly readable passwords by weaving random consonants and wovels. The generated passwords are by no means secure. I did this for an application used to launch game servers and wanted to make the passwords be easy to communicate over Skype.
-
(C#) Sort lists
Lets say you have a list of person objects (List persons) You may use Sort to update the existing list. [c language=”#”] personsList.Sort((x, y) => string.Compare(x.Name, y.Name)); [/c] or OrderBy to produce a new, ordered list. [c language=”#”] var newPersonsList = persons.OrderBy(x => x.Name); [/c]
-
(PHP) Simple pagination
Here’s a simple way to paginate when getting large amounts of posts from the database.
-
Convert seconds to days, hours, minutes and seconds
Converting seconds into days, hours, minutes and seconds int days = secondsTotal / 60 / 60 / 24; int hours = (secondsTotal / 60 / 60) % 24; int minutes = (secondsTotal / 60) % 60; int seconds = secondsTotal % 60;
-
Simple function to see if a number is Even or Odd.
Sometimes you need to check if a number is even or odd. A simple way to do this is using the modulus operator (the %-sign). It will calculate and return the remainder when dividing a value with another value. So how do you use that to find out if a number is even or odd?…
-
A couple of principles when learning how to code.
Principle 1. Always have a clear image of what you want to do BEFORE you start writing your code. Then break it down in logical instructions – making an algorithm. An algorithm is a step by step instruction on how to solve a specific problem. You might think of it as a recipe. If you…