Created
February 26, 2011 15:35
-
-
Save faisalman/845309 to your computer and use it in GitHub Desktop.
Konversi Angka ke format Rupiah & vice versa (C#, PHP, JavaScript, ActionScript 3.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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
tambahan nih untuk java
js
substr
deprecated, diganti dengan ini