Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confgen: parse number string with thousands separators (like commas or periods, such as "1,234,567.89") #157

Open
wenchy opened this issue Oct 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@wenchy
Copy link
Member

wenchy commented Oct 12, 2024

Package excelize fixes the incorrect build-in number format

commit excelize: fixes the incorrect build-in number format apply the result

Release excelize v2.7.0

Bug Fixes
Fix decimal number format round issue with build-in number format, resolve issue qax-os/excelize#1328, qax-os/excelize#1368 and qax-os/excelize#1373

Problem

Integer Float
int32 float
Integer number Float number
1,234,567 1,234,567.89

Currently, the confgen cannot parse number string with thousands separator.

Solution

  • Use strings.ReplaceAll to remove thousands separators from the string.
  • Use strconv.ParseInt for integers and strconv.ParseFloat for floating-point numbers to convert the cleaned string to a number.
  • Adjust the replacement logic based on the format of the input string (e.g., commas vs. periods).

Example: Parsing Integers with Thousands Separators

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func parseInteger(s string) (int64, error) {
	// Remove the thousands separators (commas)
	s = strings.ReplaceAll(s, ",", "")
	// Convert the cleaned string to an integer
	return strconv.ParseInt(s, 10, 64)
}

func main() {
	str := "1,234,567"
	num, err := parseInteger(str)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Parsed integer:", num)
}

Example: Parsing Floating-Point Numbers with Thousands Separators

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func parseFloat(s string) (float64, error) {
	// Remove the thousands separators (commas)
	s = strings.ReplaceAll(s, ",", "")
	// Convert the cleaned string to a float
	return strconv.ParseFloat(s, 64)
}

func main() {
	str := "1,234,567.89"
	num, err := parseFloat(str)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Parsed float:", num)
}

Handling Different Thousands Separators

If your input uses a different thousands separator (like a period), you can adjust the strings.ReplaceAll function accordingly. For example, if the input is "1.234.567,89" (common in some European countries), you would replace the periods and commas like this:

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func parseEuropeanFloat(s string) (float64, error) {
	// Replace periods with nothing (remove thousands separators)
	s = strings.ReplaceAll(s, ".", "")
	// Replace the comma with a period (decimal separator)
	s = strings.ReplaceAll(s, ",", ".")
	// Convert the cleaned string to a float
	return strconv.ParseFloat(s, 64)
}

func main() {
	str := "1.234.567,89"
	num, err := parseEuropeanFloat(str)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Parsed float:", num)
}

References

@wenchy wenchy added the enhancement New feature or request label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant