2018-01-26

Overview line 300

               {
                  const a = parseFloat(qtyOnShelf)
                  const b = parseFloat(qtyStockroom)
                  if (Number.isNaN(a) || Number.isNaN(b)) {
                    return "0.00"
                  } else {
                    return (a + b).toFixed(2)
                  }
                }}
              />

utils/formatter.ts

export const currency = (input: string | number) => {
  const price = typeof input === "number" ?
  input : parseFloat(input)
  return Number.isNaN(price) ? "" : `$${price.toFixed(2)}`
}

formatter和getter两步处理输入:

export const currency = (input: string | number) => {
  const price = typeof input === "number" ? input : parseFloat(input)
  return Number.isNaN(price) ? "" : `$${price.toFixed(2)}`
}

// step 1 any----string
// step 2 string----number

export const currency {
  formatter: (input: any) => {
    const price = typeof input === "number" ? input : parseFloat(input)
    return Number.isNaN(price) ? "" : `$${price.toFixed(2)}`
  }
  getter: (input: string) => {
    input.replace(/$/g, "")
    return parseFloat(input)
  }
}

你可能感兴趣的:(2018-01-26)