Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //PHP Terbilang (Nonrekursif) dan Roman
- static function terbilang( $x, $mode = 2, $cent = 0 ){
- $x = number_format( $x, $cent, ".", "," );
- list( $x, $y ) = explode( ".", $x . "." );
- $satuan = array( "Nol", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan" );
- $ribuan = array( 1 => "Ribu", "Juta", "Miliar", "Triliun" );
- foreach ( array_reverse( explode( ",", $x ) ) as $k => $v ){
- if ( $v == 0 ){
- if ( $x == 0 )
- $result[$k] = "Nol";
- } elseif ( $k * $v == 1 ){
- $result[$k] = "Seribu ";
- } elseif ( $v ){
- $tigaan = str_split( substr( 1000 + $v, -3 ) );
- if ( $tigaan[0] == 1 )
- $result[$k] = "Seratus ";
- elseif ( $tigaan[0] )
- $result[$k] = $satuan[$tigaan[0]] . " Ratus ";
- if ( $tigaan[1] == 1 && $tigaan[2] == 0 )
- $result[$k] .= "Sepuluh ";
- elseif ( $tigaan[1] == 1 && $tigaan[2] == 1 )
- $result[$k] .= "Sebelas ";
- elseif ( $tigaan[1] == 1 )
- $result[$k] .= $satuan[$tigaan[2]] . " Belas ";
- else {
- if ( $tigaan[1] )
- $result[$k] .= $satuan[$tigaan[1]] . " Puluh ";
- if ( $tigaan[2] )
- $result[$k] .= $satuan[$tigaan[2]] . " ";
- }
- if ( $k )
- $result[$k] .= $ribuan[ $k ] . " ";
- }
- }
- $title = trim( implode( array_reverse( $result ) ) );
- if ( 1 * $y ){
- $title .= " Koma " . $satuan[$y[0]] . " " . $satuan[$y[1]];
- }
- if ( $mode == 0 )
- return strtolower( $title );
- elseif ( $mode == 1 )
- return ucfirst( strtolower( $title ) );
- elseif ( $mode == 2 )
- return ucwords( $title );
- elseif ( $mode == 3 )
- return strtoupper( $title );
- }
- static function roman($x){
- $romans = array( 1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD',
- 100 => 'C', 90 => 'XC', 50 => 'L', 40 => 'XL', 10 => 'X',
- 9 => 'IX', 5 => 'V', 4 => 'IV', 1 => 'I' );
- $out = "";
- foreach ( $romans as $k=>$v )
- while ( $k <= $x ){
- $out .= $v;
- $x -= $k;
- }
- return $out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement