简体   繁体   English

Excel 数据未按 decimal.Decimal 类型排序

[英]Excel data not sorting the type decimal.Decimal

I have a view where i export data the numerical data but when i sort the data it is not getting sorted as the excel is not considering the values as numerical data how can i convert them to numerical data in order to display the data in numerical format and make the sorting work.i have a function which gets the data here is how it looks.我有一个视图,我将数据导出为数值数据,但是当我对数据进行排序时,它没有被排序,因为 excel 没有将这些值视为数值数据我如何才能将它们转换为数值数据以便以数值格式显示数据并进行排序。我有一个 function,它在这里获取数据是它的外观。

    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


    def get_data(self):
            data = []

        for key in self.header_keys:
            value = getattr(neutral, str(key), '-')
            val, display = self.get_output_value(key, value, user)
            values_list.append({'value': val, 'display': display})
       

        data.append({'title': user.__str__(), 'value_row': values_list})

      return data

you should wrap the data in float or int class types您应该将数据包装在 float 或 int class 类型中

eg例如

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

PROBABLY THE BETTER WAY:可能是更好的方法:

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 Decimal('.00'), Decimal('.00')

        if isinstance(value, Decimal):
            return intcomma(value.quantize(Decimal('.00'))), intcomma(display.quantize(Decimal('.00')))

        return float(value), display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM