1

Необходимо из вида цены 96 000 000 руб. получить слуховой читаемый вид, то есть так:


96 000 000 — 96 млн.
960 00 00 — 9,6 млн
96 000 — 96 000
9 600 — 9 600
960 — 960

Для решения такой задачи взял в инструменты AbbrNum jQuery plugin

Но у меня не получается убрать тыс, хочу оставить только млн,

96 000 000 — 96 млн.
9 600 000 — 9,6 млн.
96 000 — 96 тыс. <=== нужно чтоб было так: 96 000 **без префикса тыс**.
9600 — 9 600
960 — 960

как сделать с пробелами и убрать тысяча ?

ДЕМО JSFIDDLE

CПАСИБО ЗА ВНИМАНИЕ

2
  • Уберите клас у циферок, которые не хотите обрабатывать плагином
    – Oleksandr
    9 июн 2017 в 14:55
  • убрать класс не вариант смотрите на JSFIDDLE
    – Karen
    9 июн 2017 в 15:11

1 ответ 1

1

функция convert плагина, изменение в первых 4 строках

(function($) {
  $.fn.abbrNum = function(options) {

    var defaultVal = {
      decPlaces: 0
    };
    var options = $.extend(defaultVal, options);

    var convert = function(number, decPlaces) {
      var temp = number;
      number = number.replace(/\s+/g, "");
      decPlaces = Math.pow(10, decPlaces);
      if (number.length < 7) return temp;
      // Enumerate number abbreviations
      var abbrev = [" тыс", " млн", " млрд", " трлн"];

      // Go through the array backwards, so we do the largest first
      for (var i = abbrev.length - 1; i >= 0; i--) {

        // Convert array index to "1000", "1000000", etc
        var size = Math.pow(10, (i + 1) * 3);

        // If the number is bigger or equal do the abbreviation
        if (size <= number) {
          // Here, we multiply by decPlaces, round, and then divide by decPlaces.
          // This gives us nice rounding to a particular decimal place.
          number = Math.round(number * decPlaces / size) / decPlaces;

          // Handle special case where we round up to the next abbreviation
          if ((number == 1000) && (i < abbrev.length - 1)) {
            number = 1;
            i++;
          }

          // Add the letter for the abbreviation
          number += abbrev[i];

          // We are done... stop
          break;
        }
      }

      return number;
    }

    this.each(function() {
      var element = $(this).prop('tagName').toLowerCase();
      var decPlaces = options.decPlaces;
      if ($(this).attr('decplaces')) {
        decPlaces = $(this).attr('decplaces');
      }

      if (element == 'div' || element == 'span') {
        var number = $(this).html();
        number = convert(number, decPlaces);
        $(this).html(number);
      } else if (element == 'label') {
        var number = $(this).text();
        number = convert(number, decPlaces);
        $(this).text(number);
      } else if (element == 'input') {
        var number = $(this).val();
        number = convert(number, decPlaces);
        $(this).val(number);
      }
    });
  }
}(jQuery));

$(document).ready(function() {
  // no decimal places (default)
  $('.abbr-number').abbrNum();

  // decimal places as attribute
  $('.abbr-number-attr').abbrNum();

  // decimal places as option
  $('.abbr-number-opt').abbrNum({
    decPlaces: 2
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- no decimal places (default)  -->
<label class="abbr-number">96 000 000</label>
<br>

<!-- decimal places as attribute -->
<label class="abbr-number" decplaces="1">9 600000</label>
<br>

<!-- decimal places as option -->
<label class="abbr-number" decplaces="1">96 000</label>
<br>

<!-- no decimal places (default)  -->
<label class="abbr-number" decplaces="1">9 600</label>
<br>

<!-- no decimal places (default)  -->
<label class="abbr-number">960</label>
<br>

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.