Skip to content

Instantly share code, notes, and snippets.

@lhanink
Forked from faisalman/Rupiah.as
Created March 3, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhanink/2ba4806a43028a308ad7 to your computer and use it in GitHub Desktop.
Save lhanink/2ba4806a43028a308ad7 to your computer and use it in GitHub Desktop.
Konversi Angka ke format Rupiah & vice versa (C#, PHP, JavaScript, ActionScript 3.0)
/**
* 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
*/
<?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));
}
?>
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
*/
}
}
/**
* 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
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment