background

When displaying a bar chart with Echarts, you want to display data on the outside of the bar chart. By setting the label to true, it is displayed, but because echarts is internally manipulated to display the ordinates and mouse slides in thousandths of a comma separated form, you want to keep the same format for manually displayed labels.

implementation

export function toMoney(num) {
    num = parseFloat(num)
    num = num.toLocaleString()
    return num
}
Copy the code

Methods to introduce

  • num = num.toFixed(2); Converts the number to a string with 2 decimal digits, setting decimal precision, which is not required in the project

  • Num = parseFloat(num) converts a string with two decimal digits to a number with a decimal

  • num = num.toLocaleString(); Converts a number with two decimal places to the amount format

The effect

ToLocaleString learning

  • Function: Returns the string of this number in a specific locale
  • grammar
numObj.toLocaleString(string, {}])
Copy the code
  • Example 🌰
let number = 123456.789

// Currency format required
console.log(number.toLocaleString('de-DE', { style: 'currency'.currency: 'EUR' }))
Copy the code