Konversi Angka ke format Rupiah & vice versa (C#, PHP, JavaScript, ActionScript 3.0)

Konversi Angka ke format Rupiah & vice versa (C#, PHP, JavaScript, ActionScript 3.0)

Sumber : https://gist.github.com/faisalman/845309

1. convert-rupiah.js

/**
 * JavaScript Code Snippet
 * Convert Number to Rupiah & vice versa
 * https://gist.github.com/845309
 *
 * Copyright 2011-2012, Faisalman
 * Licensed under The MIT License
 * http://www.opensource.org/licenses/mit-license  
 *
 */
 
function convertToRupiah(angka)
{
 var rupiah = '';  
 var angkarev = angka.toString().split('').reverse().join('');
 for(var i = 0; i < angkarev.length; i++) if(i%3 == 0) rupiah += angkarev.substr(i,3)+'.';
 return 'Rp. '+rupiah.split('',rupiah.length-1).reverse().join('');
}
/**
 * Usage example:
 * alert(convertToRupiah(10000000)); -> "Rp. 10.000.000"
 */
 
function convertToAngka(rupiah)
{
 return parseInt(rupiah.replace(/,.*|[^0-9]/g, ''), 10);
}
/**
 * Usage example:
 * alert(convertToAngka("Rp 10.000.123")); -> 10000123
 */

2. convert_to_rupiah.php

<?php
 /**
  * PHP Code Snippet
  * Convert Number to Rupiah & vice versa
  * https://gist.github.com/845309
  *
  * Copyright 2011-2012, Faisalman
  * Licensed under The MIT License
  * http://www.opensource.org/licenses/mit-license   
  */
    
 /**  
  *
  * @param integer $angka number
  * @return string
  *
  * Usage example:
  * echo convert_to_rupiah(10000000); -> "Rp. 10.000.000"  
  */ 
 function convert_to_rupiah($angka)
 {
  return 'Rp. '.strrev(implode('.',str_split(strrev(strval($angka)),3)));
 }
 
 /**
  *
  * @param string $rupiah
  * @return integer
  *
  * Usage example:
  * echo convert_to_number("Rp. 10.000.123,00"); -> 10000123
  */   
 function convert_to_number($rupiah)
 {
  return intval(preg_replace(/,.*|[^0-9]/, '', $rupiah));
 }
 
?>

3. Rupiah.as

package
{
 /**
  * ActionScript 3.0 Code Snippet
  * Convert Number to Rupiah & vice versa
  * https://gist.github.com/845309
  *
  * Copyright 2011-2012, Faisalman
  * Licensed under The MIT License
  * http://www.opensource.org/licenses/mit-license 
  */

 public class Rupiah
 {    
  public static function convertToRupiah(angka:Number):String
  {
   var angkaRev:String = new String();
   var angkaRev2:String = new String();
   var i:Number = new Number();
   
   angkaRev = angka.toString().split('').reverse().join('');
   for(i = 0; i < angkaRev.length; i++) if(i%3 == 0) angkaRev2 += angkaRev.substr(i,3)+'.';
   return 'Rp. '+angkaRev2.split('',angkaRev2.length-1).reverse().join('');
  }  
  /**
   * // Usage example: //
   * var rp:Rupiah = new Rupiah();
   * trace(rp.convertToRupiah(10000000)); -> "Rp. 10.000.000"
   */

  public static function convertToAngka(rupiah:String):Number
  {
   return parseInt(rupiah.replace(/,.*|[^0-9]/g,''), 10);
  }
  /**
   * // Usage example: //
   * var rp:Rupiah = new Rupiah();
   * trace(rp.convertToAngka("Rp 10.000.123,00")); -> 10000123
   */ 
 }
}

4. Rupiah.cs

/**
 * C#.NET Code Snippet
 * Convert Number to Rupiah & vice versa
 * https://gist.github.com/845309
 *
 * Copyright 2012, Faisalman
 * Licensed under The MIT License
 * http://www.opensource.org/licenses/mit-license 
 */

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public static class Rupiah
{
    public static string ToRupiah(this int angka)
    {
        return String.Format(CultureInfo.CreateSpecificCulture("id-id"), "Rp. {0:N}", angka);
    }
    /**
     * // Usage example: //
     * int angka = 10000000;
     * System.Console.WriteLine(angka.ToRupiah()); // -> Rp. 10.000.000
     */

    public static int ToAngka(this string rupiah)
    {      
        return int.Parse(Regex.Replace(rupiah, @",.*|\D", ""));
    }
    /**
     * // Usage example: //
     * string rupiah = "Rp 10.000.123,00";
     * System.Console.WriteLine(rupiah.ToAngka()); // -> 10000123
     */
}

Komentar