【问题标题】:Excel data not sorting the type decimal.DecimalExcel 数据未对 decimal.Decimal 类型进行排序
【发布时间】:2022-11-21 15:43:26
【问题描述】:

我有一个视图,我将数据导出为数字数据,但是当我对数据进行排序时,它没有被排序,因为 excel 没有将这些值视为数字数据我如何才能将它们转换为数字数据以便以数字格式显示数据并进行排序。我有一个函数可以获取数据,这里是它的外观。

    def get_output_value(self, key, value, neutral=None):
        display = value
        if value is None and not user.is_active:
            return '-', '-'

        if value is None:
            return f"${Decimal('.00')}", f"${Decimal('.00')}"

        if isinstance(value, Decimal):
            return f"${intcomma(value.quantize(Decimal('.00')))}",f"${intcomma(display.quantize(Decimal('.00')))}"

        return value, display

【问题讨论】:

    标签: python python-3.x django excel openpyxl


    【解决方案1】:

    您应该将数据包装在 float 或 int 类类型中

    例如

    def get_output_value(self, key, value, neutral=None):
            display = value
            if value is None and not user.is_active:
                return 0, 0
    
            if value is None:
                return float(f"${Decimal('.00')}"), float(f"${Decimal('.00')}")
    
            if isinstance(value, Decimal):
                return float(f"${intcomma(value.quantize(Decimal('.00')))}"), float(f"${intcomma(display.quantize(Decimal('.00')))}")
    
            return float(value), display
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 2021-08-12
      • 2021-04-03
      • 2015-01-03
      • 2015-03-17
      • 2014-02-23
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多