package com.zztrapp.zztrapp.utilcode.mUtil;

import android.text.InputFilter;
import android.widget.EditText;

import com.zztrapp.zztrapp.widget.DecimalDigitsInputFilter;

import java.lang.reflect.Field;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * Describe MyUtil
 * Created by Bby on 2022/7/8
 * Email bby0856@163.com
 */
public class MyUtil {

    //十位不足补零
    public static String numFormat(int num) {
        String s = "";
        if (num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    //对象转Map
    public static HashMap<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
//        System.out.println(clazz);
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
//            if (value == null) {
//                value = "";
//            }
            map.put(fieldName, value);
        }
        return map;
    }

    //保留x位小数--经纬度 6位
    public static String getNumberPoint(double number) {
        DecimalFormat df = new DecimalFormat("#.######");
        df.setRoundingMode(RoundingMode.HALF_DOWN);
        return df.format(number);
    }

    //限制EditText输入整数位数和小数位数   经度：3 纬度：2 小数：6位
    public static void setEditTextInputNumber(EditText editText, int digitsBeforeZero) {
        editText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(digitsBeforeZero, 6)});
    }

}
