DK-Flag Erik Østergaard - Source code snippets / Kildekode småstykker Go to Home Page
 (Geometric Shapes) Source code snippets Return
  
  
Bottom of This Page

 

JavaScript

- source code snippets /
- kildekode småstykker

Education

 


(Geometric Shapes) JavaScript (JScript) source code snippets JavaScript (JScript) source code snippets / JavaScript (JScript) kildekode småstykker

- JScript is Microsoft's implementation of JavaScript. / - JScript er Microsofts udførelse af JavaScript.

Warning Warning / AdvarselWarning: Don't run the script files without reading them first! /
Warning / AdvarselAdvarsel: Kør ikke script-filerne uden at læse dem først!

JavaScript - source code snippets / JavaScript - kildekode småstykker

Round an amount off to divisible by 0.25 & Round an amount off to divisible by 0.50 / Afrund et beløb til deleligt med 0,25 & Afrund et beløb til deleligt med 0,50

Description: Round Off to 'period twenty-five' floating-point number. Rounds an amount off to divisible by 0.25 DKK. & Round Off to 'period fifty' floating-point number. Rounds an amount off to divisible by 0.50 DKK. / Beskrivelse: Afrund til 'komma femogtyve' flydende decimaltal. Afrunder et beløb til deleligt med 0,25 kr. & Afrund til 'komma halvtreds' flydende decimaltal. Afrunder et beløb til deleligt med 0,50 kr.

Developed and tested under browser(s): Microsoft Internet Explorer Version 8.x. / Udviklet og testet under browser(e): Microsoft Internet Explorer Version 8.x.

1 Implementation: (How to use:) Insert this code in the <HEAD></HEAD> section of your HTML (HyperText Markup Language) document. /
1 Implementering: (Sådan bruger du:) Indsæt denne kode i <HEAD></HEAD> sektionen af dit HTML (HyperText Markup Language) dokument.



<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript Applet and hide from old browsers -----

function formatNumTrailingDecimalZeros(floatingPointNum, len) {
  // Add missing trailing zero's after the period (.) used 
  // as decimal separator of the floating-point number.
  var numStr = "" + floatingPointNum;  // Force number to a string.
  if ( (numStr.lastIndexOf(".") == -1) && (len > 0) ) {
    numStr += ".";
  }
  while ( (numStr.length < (numStr.lastIndexOf(".") + 1 + len)) && (len > 0) ) {
    numStr += "0";
  }
  return numStr;
}

function roundit(Num, Places) {
  // Function to arithmetically round off a floating-point number to # decimal places.
  // Num: a floating-point number.
  // Places: a numerical (integer) value.
  // The formula to round any number to 'x' decimal points is:
  //   1) Multiple the original number by 10^x (10 to the power of x)
  //   2) Apply (in JavaScript) the Math.round() method to the result (any supplied 
  //      argument is rounded off to the 'nearest integer', and using the '.5' up rule)
  //   3) Divide result by 10^x
  if (Places > 0) {
    var tmpNumStr = "" + Num;  // Force number to a string.
    //var tmpNumStr = Num.toString();  // Convert number to a string.
    if ((tmpNumStr.length - tmpNumStr.lastIndexOf('.')) > (Places + 1)) {
      // Math.pow( num1, num2 )
      // pow: Returns the base argument raised to the exponent. 
      // The pow method raises 'num1', its first argument, 
      // to the power 'num2', its second argument.
      var Rounder = Math.pow(10, Places);
      return Math.round(Num * Rounder) / Rounder;  // Always rounding to the nearest integer.
    } else {
      return Num;
    }
  } else {
    return Math.round(Num);  // Always rounding to the nearest integer.
  }
}

function dkoereafrund(beloeb) {
  // Round Off to 'period twenty-five' floating-point number.
  // Rounds an amount off to divisible by 0.25 DKK.
  // Afrund til 'komma femogtyve' flydende decimaltal.
  // Afrunder et beloeb til deleligt med 0,25 kr.

  var afrbeloeb = 0;
  var resttal = 0;
  var resultatbeloeb = 0;

  // Note: All parameters (variables) passed to this function must 
  //   be valid - no error checking is performed.
  //   Moreover 'beloeb' must also be rounded off to a 'standard' 
  //   floating-point number (rounded to 2 decimal places).

  // Correction of small value errors.
  if ( (beloeb <= 0.124) && (beloeb >= -0.124) ) {
    resultatbeloeb = 0;
    // Return result.
    return resultatbeloeb;
  }

  // Rounds a given number to a specified number of decimal places
  // follow standard rules for rounding. That is, if the digit in
  // the position to the right of decimalplaces is 6 or greater, the
  // digit in the decimalplaces position is incremented by 1; if 5,
  // it becomes the nearest even number; otherwise, the digits to
  // the right of the decimalplaces are dropped. Format will use
  // the same format as that defined for positive values on negative
  // values, meaning that negative values will appear in the same
  // format as positive values.
  afrbeloeb = beloeb;

  // The modulo operator. Performs division on two numeric
  // expressions and returns only the remainder. If either of the
  // two numbers is a floating-point number, it's rounded to an
  // integer value prior to the modulo operation.
  // The remainder, expressed as an integer, is obtained (using the modulo operator).
  resttal = ( (afrbeloeb * 100) % 25 );  // Modulus.

  if ( afrbeloeb >= 0 ) {
    // Positive values. 
    if ( resttal >= 12.5 ) {
      // Range +13 to +24.
      resultatbeloeb = afrbeloeb + ((25 - resttal) / 100);
    } else {
      // Range +0 to +12.
      resultatbeloeb = afrbeloeb - ((resttal) / 100);
    }
  } else {
    // Negative values.
    if ( resttal >= -12.5 ) {
      // Range -1 to -12.
      resultatbeloeb = afrbeloeb - ((resttal) / 100);
    } else {
      // Range -13 to -24.
      resultatbeloeb = afrbeloeb + ((-25 - resttal) / 100);
    }
  }

  // Return result.
  return resultatbeloeb;
}

function dkoereafrund5(beloeb) {
  // Round Off to 'period fifty' floating-point number.
  // Rounds an amount off to divisible by 0.50 DKK.
  // Afrund til 'komma halvtreds' flydende decimaltal.
  // Afrunder et beloeb til deleligt med 0,50 kr.

  var afrbeloeb = 0;
  var resttal = 0;
  var resultatbeloeb = 0;

  // Note: All parameters (variables) passed to this function must 
  //   be valid - no error checking is performed.
  //   Moreover 'beloeb' must also be rounded off to a 'standard' 
  //   floating-point number (rounded to 2 decimal places).

  // Correction of small value errors.
  if ( (beloeb <= 0.254) && (beloeb >= -0.254) ) {
    resultatbeloeb = 0;
    // Return result.
    return resultatbeloeb;
  }

  // Rounds a given number to a specified number of decimal places
  // follow standard rules for rounding. That is, if the digit in
  // the position to the right of decimalplaces is 6 or greater, the
  // digit in the decimalplaces position is incremented by 1; if 5,
  // it becomes the nearest even number; otherwise, the digits to
  // the right of the decimalplaces are dropped. Format will use
  // the same format as that defined for positive values on negative
  // values, meaning that negative values will appear in the same
  // format as positive values.
  afrbeloeb = beloeb;

  // The modulo operator. Performs division on two numeric
  // expressions and returns only the remainder. If either of the
  // two numbers is a floating-point number, it's rounded to an
  // integer value prior to the modulo operation.
  // The remainder, expressed as an integer, is obtained (using the modulo operator).
  resttal = ( (afrbeloeb * 100) % 50 );  // Modulus.

  if ( afrbeloeb >= 0 ) {
    // Positive values. 
    if ( resttal >= 25.5 ) {
      // Range +26 to +49.
      resultatbeloeb = afrbeloeb + ((50 - resttal) / 100);
    } else {
      // Range +0 to +25.
      resultatbeloeb = afrbeloeb - ((resttal) / 100);
    }
  } else {
    // Negative values.
    if ( resttal >= -25.5 ) {
      // Range -1 to -25.
      resultatbeloeb = afrbeloeb - ((resttal) / 100);
    } else {
      // Range -26 to -49.
      resultatbeloeb = afrbeloeb + ((-50 - resttal) / 100);
    }
  }

  // Return result.
  return resultatbeloeb;
}

// - End of JavaScript code and done hiding -->
</SCRIPT>

Conventions used for: Source code syntax highlighting. / Regler brugt til: Kildekode syntaks fremhævning.

2 Implementation: (How to use:) See the page mentioned below and see its JavaScript by using View Source. /
2 Implementering: (Sådan bruger du:) Se den side, der er nævnt nedenfor og se dens JavaScript ved at bruge Vis Kilde.

...extract: / ...uddrag:



<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript Applet and hide from old browsers -----

var decimal_figure = parseFloat(your_floating-point_number_goes_here / dit_flydende_decimaltal_indsaettes_her);  // Force string to a number.

var ro_twenty_five_FPN = decimal_figure; // Stores Round Off 'period twenty-five' floating-point number.
var ro_fifty_FPN = decimal_figure; // Stores Round Off 'period fifty' floating-point number.

ro_twenty_five_FPN = roundit(ro_twenty_five_FPN, 2); // Round Off to 'standard' floating-point number (rounded to 2 decimal places).
ro_fifty_FPN = roundit(ro_fifty_FPN, 2); // Round Off to 'standard' floating-point number (rounded to 2 decimal places).

ro_twenty_five_FPN = dkoereafrund(ro_twenty_five_FPN); // Round Off to 'period twenty-five' floating-point number.
ro_fifty_FPN = dkoereafrund5(ro_fifty_FPN); // Round Off to 'period fifty' floating-point number.

ro_twenty_five_FPN = roundit(ro_twenty_five_FPN, 2); // Round Off to 'standard' floating-point number (rounded to 2 decimal places).
ro_fifty_FPN = roundit(ro_fifty_FPN, 2); // Round Off to 'standard' floating-point number (rounded to 2 decimal places).

var ro_twenty_five_Str = formatNumTrailingDecimalZeros(ro_twenty_five_FPN, 2); // Add missing trailing zero's after the period (.) used as decimal separator of the floating-point number.
var ro_fifty_Str = formatNumTrailingDecimalZeros(ro_fifty_FPN, 2); // Add missing trailing zero's after the period (.) used as decimal separator of the floating-point number.

// - End of JavaScript code and done hiding -->
</SCRIPT>

Conventions used for: Source code syntax highlighting. / Regler brugt til: Kildekode syntaks fremhævning.

You can try the source code here: / Du kan afprøve kildekoden her:

Round Off / AfrundOpen this link in new window / Åben dette link i nyt vindue

or... / eller...

See similar source code for Basic & VBA here: / Se tilsvarende kildekode for Basic & VBA her:

[Basic & VBA] Round an amount off to divisible by 0.25 & Round an amount off to divisible by 0.50 / [Basic & VBA] Afrund et beløb til deleligt med 0,25 & Afrund et beløb til deleligt med 0,50


(Geometric Shapes) Source code snippetsSource code snippets / Kildekode småstykker

- often included as functions for use in modules with program code, macros, and scripts etcetera. / - mange gange inkluderet som funktioner til brug i moduler med programkode, makroer og scripts og så videre.

The code might need some minor tweaks to run in your application. / Koden kan behøve nogle mindre ændringer for at kunne afvikles i dit anvendelsesområde.

Warning / Advarsel Licence: Free to use, but please share improvements. No warranty - use at own risk. /
Warning / Advarsel Licens: Fri brug, men del venligst forbedringer. Ingen garanti - brug på eget ansvar.

Warning: Don't run the script files without reading them first!
Total absence of any guarantee, warranty, or responsibility for script files, the script(s), the files they may produce, or the effects the script(s) or script-produced files may have. The script(s) is, after all, plain text. The burden is on the person using the script(s) to examine the script source code and determine whether or not a script is usable and safe. Operating systems and browsers are constantly changing. What works today may not work tomorrow!

Advarsel: Kør ikke script-filerne uden at læse dem først!
Totalt fravær af nogen form for garanti, garanti eller ansvar for script-filer, scriptet(scriptene), de filer, de kan producere eller de virkninger, scriptet(scriptene) eller scriptproducerede filer kan have. Scriptet(Scriptene) er, trods alt, almindelig tekst. Byrden er på brugeren af scriptet(scriptene) til at undersøge script kildekoden og afgøre, hvorvidt et script er brugbart og sikkert. Operativsystemer og browsere er under konstant forandring. Hvad fungerer i dag, fungerer muligvis ikke i morgen!


   Top of This Page
   Return
   Go to Home Page