python代码实现将json中所有字段四舍五入保留n位小数

json中数据不规范,有时我们需要将数值(字符串类型的数值)统一保留多少位小数。

以下是我的代码,使用递归算法遍历json中所有的层。只要发现value值为浮点型或可转换成数值型的字符串则全部转换成浮点数;

具体代码如下:

class RoundJSON(object):

#data:需要处理的目标json,digit为保留多少位小数位默认保留4位

	def round_data(self,data={},digit=4):#将data中的所有字段只要是数值则四舍五入
	    if(type(data)==type({})):
	        keys=data.keys()
	        if (len(keys) > 0 and type(keys)!=type(None)):
	            for key in keys:
	                value=data.get(key)
	                isnum=self.is_number(value)
	                if(isnum==True):
	                    value = self.roundUp(value, digit)
	                    data[key] = value
	                elif(type(value)==type({}) or type(value)==type([])):
	                    self.round_data(value, digit)
	    elif(type(data)==type([])):
	        for i in range(len(data)):
	            if (type(data[i]) == type({})):
	                keys = data[i].keys()
	                if(len(keys)>0 and type(keys)!=type(None)):
	                    for key in keys:
	                        value = data[i].get(key)
	                        if (self.is_number(value) == True):
	                            value = self.roundUp(value, digit)
	                            data[i][key] = value
	                        elif(type(value)==type({}) or type(value)==type([])):
	                            self.round_data(value, digit)
	            elif(type(data[i]) == type([]) and len(data[i])>0):
	                for value in data[i]:
	                    if (self.is_number(value) == True):
	                        value_new = self.roundUp(value, digit)
	                        data[i]=[value_new if x==value else x for x in data[i]]
	                    elif (type(value) == type({}) or type(value) == type([])):
	                        self.round_data(value, digit)
	            else:
	                if (self.is_number(data[i]) == True):
	                    value_new = self.roundUp(data[i], digit)
	                    data=[value_new if x==data[i] else x for x in data]
	                elif (type(data[i]) == type({}) or type(data[i]) == type([])):
	                    self.round_data(data[i], digit)
	    return data
	def is_number(self,s):  #判断字符串s是否是数值
	    try:
	        float(s)
	        return True
	    except Exception as e:
	        pass
	    try:
	        unicodedata.numeric(s)
	        return True
	    except Exception as e:
	        pass
	    return False
	
	def roundUp(self, value, digit):
	    flag = self.is_number(value)
	    if (flag == True and 'e' not in str(value)): #排除科学计数法
	        result = str(value)
	        if (float(value) < 0):
	            result = result[1:]
	            if (result != ''):
	                indexDec = result.find('.')
	                if (indexDec > 0):
	                    decimal = result[indexDec + 1:]
	                    decimalCount = len(decimal)
	                    if (decimalCount > digit):
	                        xiaoshu = result[indexDec + digit + 1]  # 第digit+1位小数
	                        if (int(xiaoshu) > 4):
	                            result = str(float(value) * -1 + pow(10, digit * -1))
	                            # 存在进位的可能,小数点会移位
	                            indexDec = result.find('.')
	                            result = result[:indexDec + digit + 1]
	                        else:
	                            result = result[:indexDec + digit + 1]
	                    else:
	                        lens = digit - len(result[indexDec:]) + 1
	                        for i in range(lens):
	                            result += '0'
	            result = float(result) * -1
	            return result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值