-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added getcurrencysymbol, convert_to_major & minor_unit functionality
- Loading branch information
1 parent
03b688c
commit 36bfc9a
Showing
7 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package currency provides functionality to handle information about currencies. | ||
package currency | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
|
||
"github.com/razorpay/i18nify/packages/i18nify-go/modules/currency" | ||
) | ||
|
||
// ConvertToMajorUnit converts an amount from a minor currency unit to a major currency unit. | ||
// For example, 100 cents to 1 dollar. | ||
func ConvertToMajorUnit(code string, amount float64) (float64, error) { | ||
// Get currency information for the specified code. | ||
currencyInfo, err := currency.GetCurrencyInformation(code) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// Convert MinorUnit to float64. | ||
minorUnit, err := strconv.ParseFloat(currencyInfo.MinorUnit, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err) | ||
} | ||
|
||
// Calculate the minor unit multiplier to float64 | ||
minorUnitMultiplier := math.Pow(10, minorUnit) | ||
if minorUnitMultiplier <= 0 { | ||
minorUnitMultiplier = 100 // Default fallback value. | ||
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code) | ||
} | ||
// 100 = Default fallback value. | ||
|
||
// Convert the amount from minor to major unit. | ||
majorUnitAmount := amount / minorUnitMultiplier | ||
|
||
return majorUnitAmount, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package currency provides functionality to handle information about currencies. | ||
package currency | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
|
||
"github.com/razorpay/i18nify/packages/i18nify-go/modules/currency" | ||
) | ||
|
||
// ConvertToMinorUnit converts an amount from a major currency unit to a minor currency unit. | ||
// For example, 1 dollar to 100 cents. | ||
func ConvertToMinorUnit(code string, amount float64) (float64, error) { | ||
// Get currency information for the specified code. | ||
currencyInfo, err := currency.GetCurrencyInformation(code) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// Convert MinorUnit to float64. | ||
minorUnit, err := strconv.ParseFloat(currencyInfo.MinorUnit, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err) | ||
} | ||
|
||
// Calculate the minor unit multiplier to float64 | ||
minorUnitMultiplier := math.Pow(10, minorUnit) | ||
if minorUnitMultiplier <= 0 { | ||
minorUnitMultiplier = 100 // Default fallback value. | ||
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code) | ||
} | ||
// 100 = Default fallback value. | ||
|
||
// Convert the amount from minor to major unit. | ||
minorUnitAmount := amount * minorUnitMultiplier | ||
|
||
return minorUnitAmount, nil | ||
} |
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 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