Skip to content

Instantly share code, notes, and snippets.

@faisalman
Created February 26, 2011 15:35
  • Star 62 You must be signed in to star a gist
  • Fork 36 You must be signed in to fork a gist
Star You must be signed in to star a gist
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)
/**
* 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
*/
}
@azinudinachzab
Copy link

makasih bang

@miftakhulrobi
Copy link

Bagaimana cara mengembalikan 10.000 menjadi 10000

@tablor1453
Copy link

@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

@aacassandra
Copy link

mantap vroh

@hstaprl
Copy link

hstaprl commented Jan 20, 2022

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

@aacassandra
Copy link

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

@daudtribakti
Copy link

mantap boby

@farishan
Copy link

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
Copy link

dimaslanjaka commented Feb 2, 2024

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 : '') + ',-';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment