Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
*/
}
@anovsiradj
Copy link

anovsiradj commented Dec 16, 2015

thanks buat yg JS. 🍰

sama saran di-line: https://gist.github.com/faisalman/845309#file-convert-rupiah-js-L17
dijadikan:

rupiah = rupiah.split('',rupiah.length-1).reverse().join('');
return 'Rp. '+(rupiah.length < 1 ? '0' : rupiah)+',-';

handle kalo string kosong, akan otomatis Rp. 0,-

@lhanink
Copy link

lhanink commented Mar 3, 2016

bro mau nanya nih,, bagaimana menerapkan kode ini jika value untuk text berasal dari php $_POST ?

mohon bantuannya, cz gk terlalu ngerti soal js :(

@amry666
Copy link

amry666 commented Apr 21, 2016

assalamualaikum man , kalo decimal nya kenapa ,00 terus ya?

@anovsiradj
Copy link

anovsiradj commented Jun 12, 2016

@lhanink berarti situ pake yg versi php aja

@abon07
Copy link

abon07 commented Jul 24, 2016

maaf mau nanya masih newbie banget
itu yang php penerapan ke textbox nya gimana ya?

@tejoprabowo
Copy link

tejoprabowo commented Sep 8, 2017

makasih buat yang php

@kripul
Copy link

kripul commented Oct 28, 2017

tengkyu mas e, kamu menyelamatkan hidupku

@sf1224mat
Copy link

sf1224mat commented Feb 15, 2018

Thanks om
semoga berkah ilmunya :)

@rommyarb
Copy link

rommyarb commented Mar 6, 2018

Thanks broder

@lutfiadnan92
Copy link

lutfiadnan92 commented May 4, 2018

kenapa format js kalau melebihi satu juta menjadi NaN?

@srifqi
Copy link

srifqi commented Jul 28, 2018

Kau bisa ganti biar ikut PUEBI, @faisalman?

@muhammadfawwaz
Copy link

muhammadfawwaz commented Aug 31, 2018

thanks vroh

@enix-app
Copy link

enix-app commented Sep 14, 2018

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

@imamsan
Copy link

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

BagasNS commented Oct 28, 2018

lebih simple dibanding bawaan fungsi php..thx

@do4Mother
Copy link

do4Mother commented Jan 24, 2019

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

@SunDi3yansyah
Copy link

SunDi3yansyah commented Jan 30, 2019

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

@azinudinachzab
Copy link

azinudinachzab commented Jun 12, 2019

makasih bang

@miftakhulrobi
Copy link

miftakhulrobi commented Jan 11, 2020

Bagaimana cara mengembalikan 10.000 menjadi 10000

@tablor1453
Copy link

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

@aacassandra
Copy link

aacassandra commented Mar 15, 2021

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

aacassandra commented Jan 20, 2022

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

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