Instantly share code, notes, and snippets.

# faisalman/Rupiah.as

Created February 26, 2011 15:35
Star You must be signed in to star a gist
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
 "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 */ }

### tejoprabowo commented Sep 8, 2017

makasih buat yang php

### kripul commented Oct 28, 2017

tengkyu mas e, kamu menyelamatkan hidupku

### sf1224mat commented Feb 15, 2018

Thanks om
semoga berkah ilmunya :)

Thanks broder

### lutfiadnan92 commented May 4, 2018

kenapa format js kalau melebihi satu juta menjadi NaN?

### srifqi commented Jul 28, 2018

Kau bisa ganti biar ikut PUEBI, @faisalman?

thanks vroh

### enix-app commented Sep 14, 2018

Penulisan yang benar `Rp123.456,67` ga pake spasi titik.

### imamsan commented Oct 7, 2018

Buat ngilangin desimal 2 angka dibelakang ditambah 0 setelah N
`return String.Format(CultureInfo.CreateSpecificCulture("id-id"), "Rp. {0:N0}", angka);`

### BagasNS commented Oct 28, 2018 • edited

lebih simple dibanding bawaan fungsi php..thx

### do4Mother commented Jan 24, 2019

selama buat project selalu pakai cara ini. terima kasih banyak gan. sangat membantu sekali

### SunDi3yansyah commented Jan 30, 2019

Penulisan rupiah yang benar adalah `Rp1.000.000` atau `Rp1.000.000.00` atau `Rp1.000.000,-`

makasih bang

### miftakhulrobi commented Jan 11, 2020

Bagaimana cara mengembalikan 10.000 menjadi 10000

### tablor1453 commented Dec 31, 2020

@miftakhulrobi cara sederhanya bisa seperti ini:

1. angka/nilainya dikonvesi kedalam string
2. split jadi array dengan separator . (titik)
3. gabungkan array dan ditampung kedalam variable
4. konversi variable tersebut kedalam angka

mantap vroh

### hstaprl commented Jan 20, 2022

kl sy tambahkan symbol minus, contoh hasil nya '-.888' itu bagaimana ya kak?

### aacassandra commented Jan 20, 2022

@hstaprl maksut kamu -0.888 gitu? kok punyamu tanpa 0? why?

mantap boby

### farishan commented Sep 16, 2023

Terima kasih fungsinya, Faisalman! Saya titip editan di sini ya:

```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('');
}```

### dimaslanjaka commented Feb 2, 2024 • edited

tambahan nih untuk java

```import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class RupiahUtils {
public static void main(String[] args) {
double harga = 250000000;
System.out.printf("Harga Rupiah: %s %n", convertToRupiah(harga));
System.out.printf("%.2f", harga);
}

public static String convertToRupiah(Double angka) {
DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

formatRp.setCurrencySymbol("Rp. ");
formatRp.setMonetaryDecimalSeparator(',');
formatRp.setGroupingSeparator('.');

kursIndonesia.setDecimalFormatSymbols(formatRp);
return kursIndonesia.format(angka);
}

public static String convertToRupiahWithoutTrailingZeros(Double angka) {
final String regex = ",00\$";
final String subst = ",-";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(convertToRupiah(angka));

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
return result;
}
}```

js `substr` deprecated, diganti dengan ini

```function convertToRupiah(angka) {
let number_string = new String(angka).replace(/[^,\d]/g, '').toString(),
split = number_string.split(','),
sisa = split[0].length % 3,
rupiah = split[0].substring(0, sisa),
ribuan = split[0].substring(sisa).match(/\d{3}/gi);

// tambahkan koma jika yang di input sudah menjadi angka ribuan
if (ribuan) {
separator = sisa ? ',' : '';
rupiah += separator + ribuan.join(',');
}

rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
return (rupiah ? 'Rp. ' + rupiah : '') + ',-';
}```