Skip to content

Instantly share code, notes, and snippets.

@White2001Offl
Created July 29, 2021 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save White2001Offl/24962dacb69bff2015fddec1f65a24ff to your computer and use it in GitHub Desktop.
Save White2001Offl/24962dacb69bff2015fddec1f65a24ff to your computer and use it in GitHub Desktop.
Convert a large floating number to currency readable string
/*
This is about format a large floating number to a readable currency string
Ex - 1684748483.3454
Output - 1,684,748,483.34
You can customize the decimals part to your needs.
*/
func FormatString(number float64) string{
data := fmt.Sprintf("%.2f",number)
temp := strings.Split(data,".")
main := temp[0]
fraction := temp[1]
var mainData string
if len(main) < 3{
return main + "." + fraction
} else{
for i := len(main) - 1; i>= 0; i--{
temp := len(main) - i -1
if temp % 3 == 0 && temp != 0{
mainData = string(main[i]) + "," + mainData
} else{
mainData = string(main[i]) + mainData
}
}
return mainData + "." + fraction
}
}
@White2001Offl
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment