Created
July 29, 2021 08:12
-
-
Save White2001Offl/24962dacb69bff2015fddec1f65a24ff to your computer and use it in GitHub Desktop.
Convert a large floating number to currency readable string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/PbPUAyf1tY3