Tolong pilih kategori sesuai, jenis posting (diskusi atau bukan) dan sertakan tag/topik yang sesuai seperti komputer, java, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus!
- Bagi Anda yang ingin mendaftar, baca link berikut:
http://diskusiweb.com/discussion/50491/how-to-registrasi-diskusiweb-com-baca-ini-terlebih-dahulu
- Cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com
- Cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

Pembulatan Angka

Halo semua saya ada permasalahan untuk pembulatan dengan ketentuan sebagai berikut,
0-49 pembulatan kebawah jadi nol
50-99 pembulatan keatas jadi 100

misal ada nilai seperti ini :
549480
saya pake function number_format menjadi
549,480
nah ini dibulatkan menjadi
549,500

nah function di php untuk pembulatan seperti itu apa ya? saya coba pake floor,round itu ga ngerubah yang belakangnya itu. terima kasih atas bantuannya.

salam.
Tagged:

Comments

  • Gue sendiri kyaknya belum pernah ngelihat pembulatan klo masih didepan koma. mungkin bisa pake function if kali kang.<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="php"><span class="re0">$x</span> <span class="sy0">=</span> <span class="nu0">549480</span><span class="sy0">;</span>
    <span class="re0">$lx</span> <span class="sy0">=</span> <span class="kw3">substr</span><span class="br0">(</span><span class="re0">$x</span><span class="sy0">,-</span><span class="nu0">2</span><span class="sy0">,</span><span class="nu0">2</span><span class="br0">)</span><span class="sy0">;</span>
     
    <span class="kw1">echo</span> <span class="re0">$x</span><span class="sy0">;</span>
    <span class="kw1">echo</span> <span class="st0">"<br />"</span><span class="sy0">;</span>
     
    <span class="kw1">if</span><span class="br0">(</span><span class="re0">$lx</span><span class="sy0">></span><span class="nu0">49</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="re0">$lx</span> <span class="sy0">=</span> <span class="nu0">100</span><span class="sy0">-</span><span class="re0">$lx</span><span class="sy0">;</span>
    <span class="re0">$x</span> <span class="sy0">=</span> <span class="re0">$x</span><span class="sy0">+</span><span class="re0">$lx</span><span class="sy0">;</span>
    <span class="br0">}</span><span class="kw1">else</span><span class="br0">{</span>
    <span class="re0">$x</span> <span class="sy0">=</span> <span class="re0">$x</span><span class="sy0">-</span><span class="re0">$lx</span><span class="sy0">;</span>
    <span class="br0">}</span>
     
    <span class="kw1">echo</span> <span class="re0">$x</span><span class="sy0">;</span></pre></div></td></tr></table></div>
  • edited May 2011
    Biasanya sih gw pakai ini (misal : bilangan pecahan) :<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="php"><span class="re0">$contoh_angkanya</span> <span class="sy0">=</span> <span class="nu19">1.5</span><span class="sy0">;</span>
    <span class="re0">$hasilnya</span> <span class="sy0">=</span> <span class="kw3">intval</span><span class="br0">(</span><span class="re0">$contoh_angkanya</span> <span class="br0">)</span> <span class="sy0">+</span> <span class="nu0">1</span><span class="sy0">;</span>
     
    <span class="kw1">echo</span> <span class="re0">$hasilnya</span><span class="sy0">;</span> <span class="sy0"><--</span> Hasilnya adalah <span class="nu0">2</span></pre></div></td></tr></table></div>
  • edited May 2011
    Ini script lengkapnya gan + fungsi format numbernya juga ada..,
    Silahkan dituangkan dan saya jamin pasti bisa.. . .. . :-bd<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="php"><span class="sy1"><?</span>
    <span class="kw2">function</span> BulatKoma<span class="br0">(</span><span class="re0">$angka</span><span class="br0">)</span><span class="br0">{</span>
    <span class="re0">$kelipatan</span> <span class="sy0">=</span> <span class="nu0">100</span><span class="sy0">;</span>
    <span class="re0">$sisa</span> <span class="sy0">=</span> <span class="re0">$angka</span> <span class="sy0">%</span> <span class="re0">$kelipatan</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">(</span><span class="re0">$sisa</span> <span class="sy0"><</span> <span class="nu0">50</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="re0">$hasilBulat</span> <span class="sy0">=</span> <span class="re0">$angka</span> <span class="sy0">-</span> <span class="re0">$sisa</span><span class="sy0">;</span>
    <span class="br0">}</span>
    <span class="kw1">else</span> <span class="br0">{</span>
    <span class="re0">$kekurangan</span> <span class="sy0">=</span> <span class="re0">$kelipatan</span> <span class="sy0">-</span> <span class="re0">$sisa</span><span class="sy0">;</span>
    <span class="re0">$hasilBulat</span> <span class="sy0">=</span> <span class="re0">$angka</span> <span class="sy0">+</span> <span class="re0">$kekurangan</span><span class="sy0">;</span>
    <span class="br0">}</span>
    <span class="kw3">floor</span><span class="br0">(</span><span class="re0">$hasilBulat</span><span class="br0">)</span><span class="sy0">;</span>
     
    <span class="co1">//BERI KOMA UNTUK NOMINAL UANG</span>
    <span class="re0">$data</span> <span class="sy0">=</span> <span class="re0">$hasilBulat</span><span class="sy0">;</span>
    <span class="re0">$jml</span> <span class="sy0">=</span> <span class="kw3">strlen</span><span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="kw1">while</span><span class="br0">(</span><span class="re0">$jml</span> <span class="sy0">></span> <span class="nu0">3</span><span class="br0">)</span><span class="br0">{</span>
    <span class="re0">$hasil</span> <span class="sy0">=</span> <span class="st0">","</span> <span class="sy0">.</span> <span class="kw3">substr</span><span class="br0">(</span><span class="re0">$data</span><span class="sy0">,-</span><span class="nu0">3</span><span class="br0">)</span> <span class="sy0">.</span> <span class="re0">$hasil</span><span class="sy0">;</span>
    <span class="re0">$l</span> <span class="sy0">=</span> <span class="kw3">strlen</span><span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span> <span class="sy0">-</span> <span class="nu0">3</span><span class="sy0">;</span>
    <span class="re0">$data</span> <span class="sy0">=</span> <span class="kw3">substr</span><span class="br0">(</span><span class="re0">$data</span><span class="sy0">,</span><span class="nu0">0</span><span class="sy0">,</span><span class="re0">$l</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="re0">$jml</span> <span class="sy0">=</span> <span class="kw3">strlen</span><span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="br0">}</span>
    <span class="re0">$hasil</span> <span class="sy0">=</span> <span class="re0">$data</span> <span class="sy0">.</span> <span class="re0">$hasil</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="re0">$hasil</span><span class="sy0">;</span>
    <span class="br0">}</span>
     
    <span class="co1">//SEKARANG ANDA COBA SENDIRI</span>
    <span class="re0">$angka</span> <span class="sy0">=</span> <span class="nu0">549480</span><span class="sy0">;</span>
    <span class="kw1">echo</span> BulatKoma<span class="br0">(</span><span class="re0">$angka</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="sy1">?></span></pre></div></td></tr></table></div>
  • ribet amat

    number_format( round( $angka , -2 ) , 0 , '.' , ',' );
  • edited May 2011
    @^ Mantap!
  • edited May 2011
    ribet amat

    number_format( round( $angka , -2 ) , 0 , '.' , ',' );
    Hehhehe., bener juga yak., kenapa gak kepikir sampe kesana.. :-))
  • <?

    $contoh_angkanya = 1111;
    $hasilnya = number_format( round( $contoh_angkanya , -2 ) , 0 , '.' , ',' );
     
    echo $hasilnya; //<-- Hasilnya adalah 2-->

    ?>


    Hasilnya adalah :1,100





    Kalau kita menginginkan pembulatan ke atas gimana ?
    Hasil yg kami inginkan dari perintah di atas : 1,200






Sign In or Register to comment.