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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

amry666 commented Apr 21, 2016

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

@anovsiradj

This comment has been minimized.

Copy link

anovsiradj commented Jun 12, 2016

@lhanink berarti situ pake yg versi php aja

@abon07

This comment has been minimized.

Copy link

abon07 commented Jul 24, 2016

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

@tejoprabowo

This comment has been minimized.

Copy link

tejoprabowo commented Sep 8, 2017

makasih buat yang php

@kripul

This comment has been minimized.

Copy link

kripul commented Oct 28, 2017

tengkyu mas e, kamu menyelamatkan hidupku

@sf1224mat

This comment has been minimized.

Copy link

sf1224mat commented Feb 15, 2018

Thanks om
semoga berkah ilmunya :)

@rommyarb

This comment has been minimized.

Copy link

rommyarb commented Mar 6, 2018

Thanks broder

@lutfiadnan92

This comment has been minimized.

Copy link

lutfiadnan92 commented May 4, 2018

kenapa format js kalau melebihi satu juta menjadi NaN?

@srifqi

This comment has been minimized.

Copy link

srifqi commented Jul 28, 2018

Kau bisa ganti biar ikut PUEBI, @faisalman?

@muhammadfawwaz

This comment has been minimized.

Copy link

muhammadfawwaz commented Aug 31, 2018

thanks vroh

@enix-app

This comment has been minimized.

Copy link

enix-app commented Sep 14, 2018

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

@imamsan

This comment has been minimized.

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

This comment has been minimized.

Copy link

BagasNS commented Oct 28, 2018

lebih simple dibanding bawaan fungsi php..thx

@do4Mother

This comment has been minimized.

Copy link

do4Mother commented Jan 24, 2019

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

@SunDi3yansyah

This comment has been minimized.

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

This comment has been minimized.

Copy link

azinudinachzab commented Jun 12, 2019

makasih bang

@miftakhulrobi

This comment has been minimized.

Copy link

miftakhulrobi commented Jan 11, 2020

Bagaimana cara mengembalikan 10.000 menjadi 10000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.