Tuesday, September 29, 2015

Hàm PHP đọc số tiền thành chữ

Hàm PHP đọc số thành chữ (tiền)
function convert_number_to_words($number)
{
  $hyphen = ' ';
  $conjunction = '  ';
  $separator = ' ';
  $negative = 'âm ';
  $decimal = ' phẩy ';
  $dictionary = array(
    0 => 'không',
    1 => 'một',
    2 => 'hai',
    3 => 'ba',
    4 => 'bốn',
    5 => 'năm',
    6 => 'sáu',
    7 => 'bảy',
    8 => 'tám',
    9 => 'chín',
    10 => 'mười',
    11 => 'mười một',
    12 => 'mười hai',
    13 => 'mười ba',
    14 => 'mười bốn',
    15 => 'mười năm',
    16 => 'mười sáu',
    17 => 'mười bảy',
    18 => 'mười tám',
    19 => 'mười chín',
    20 => 'hai mươi',
    30 => 'ba mươi',
    40 => 'bốn mươi',
    50 => 'năm mươi',
    60 => 'sáu mươi',
    70 => 'bảy mươi',
    80 => 'tám mươi',
    90 => 'chín mươi',
    100 => 'trăm',
    1000 => 'ngàn',
    1000000 => 'triệu',
    1000000000 => 'tỷ',
    1000000000000 => 'nghìn tỷ',
    1000000000000000 => 'ngàn triệu triệu',
    1000000000000000000 => 'tỷ tỷ'
  );
  if (!is_numeric($number)) {
    return false;
  }

  if (($number >= 0 && (int)$number < 0) || (int)$number < 0 - PHP_INT_MAX) {

    // overflow

    trigger_error('convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING);
    return false;
  }

  if ($number < 0) {
    return $negative . convert_number_to_words(abs($number));
  }

  $string = $fraction = null;
  if (strpos($number, '.') !== false) {
    list($number, $fraction) = explode('.', $number);
  }

  switch (true) {
  case $number < 21:
    $string = $dictionary[$number];
    break;

  case $number < 100:
    $tens = ((int)($number / 10)) * 10;
    $units = $number % 10;
    $string = $dictionary[$tens];
    if ($units) {
      $string.= $hyphen . $dictionary[$units];
    }

    break;

  case $number < 1000:
    $hundreds = $number / 100;
    $remainder = $number % 100;
    $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
    if ($remainder) {
      $string.= $conjunction . convert_number_to_words($remainder);
    }

    break;

  default:
    $baseUnit = pow(1000, floor(log($number, 1000)));
    $numBaseUnits = (int)($number / $baseUnit);
    $remainder = $number % $baseUnit;
    $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
    if ($remainder) {
      $string.= $remainder < 100 ? $conjunction : $separator;
      $string.= convert_number_to_words($remainder);
    }

    break;
  }

  if (null !== $fraction && is_numeric($fraction)) {
    $string.= $decimal;
    $words = array();
    foreach(str_split((string)$fraction) as $number) {
      $words[] = $dictionary[$number];
    }

    $string.= implode(' ', $words);
  }

  return $string;
}

Monday, September 28, 2015

Hàm javascript đọc số thành chữ

/**
* Đọc số thành chữ (Vietnamese)
* DOCSO.doc( 123 );
*/
var DOCSO=function(){var t=["không","một","hai","ba","bốn","năm","sáu","bảy","tám","chín"],r=function(r,n){var o="",a=Math.floor(r/10),e=r%10;return a>1?(o=" "+t[a]+" mươi",1==e&&(o+=" mốt")):1==a?(o=" mười",1==e&&(o+=" một")):n&&e>0&&(o=" lẻ"),5==e&&a>=1?o+=" lăm":4==e&&a>=1?o+=" tư":(e>1||1==e&&0==a)&&(o+=" "+t[e]),o},n=function(n,o){var a="",e=Math.floor(n/100),n=n%100;return o||e>0?(a=" "+t[e]+" trăm",a+=r(n,!0)):a=r(n,!1),a},o=function(t,r){var o="",a=Math.floor(t/1e6),t=t%1e6;a>0&&(o=n(a,r)+" triệu",r=!0);var e=Math.floor(t/1e3),t=t%1e3;return e>0&&(o+=n(e,r)+" ngàn",r=!0),t>0&&(o+=n(t,r)),o};return{doc:function(r){if(0==r)return t[0];var n="",a="";do ty=r%1e9,r=Math.floor(r/1e9),n=r>0?o(ty,!0)+a+n:o(ty,!1)+a+n,a=" tỷ";while(r>0);return n.trim()}}}();

Cách dùng

alert( DOCSO.doc(123000) );

Friday, September 11, 2015

Vô hiệu Xem trước In (Print review) của chrome

Tạo 1 shortcut của Chrome mới với Target: "C:\Program Files\Google\Chrome\Application\chrome.exe" --disable-print-preview

Tham số --disable-print-preview sẽ vô hiệu hóa xem trước khi in. Bấm in sẽ bật ra giao diện chọn máy in để in.

Nếu thay bằng tham số --kiosk-printing sẽ in luôn mà không cần hỏi chọn máy in nào. Máy in mặc định được in sẽ là máy in trước đó bạn chọn. Tham số rất hữu ích để triển khai hệ thống POS bán hàng... k phải mất thời gian xác nhận máy in.

Và tham số --kiosk sẽ cho phép Chrome chạy ở trạnh thái Full Screen.

Chú ý:
- Thoát Chrome hoàn toàn mà mở lại bằng shortcut mới thì tham số mới có hiệu lực.
- Đường dẫn đến ứng dụng Chrome thay đổi tùy theo máy tính của bạn.