Skip to content

Commit

Permalink
Fix Ledger exercise NewLine & CultureInfo issues (#1275)
Browse files Browse the repository at this point in the history
* Fix Ledger exercise NewLine & CultureInfo issues

- Replace System.Environment.NewLine with \n
- Use new CultureInfo(locale, false) instead
- Add currencyNegativePattern function for the tests

* Replace \n instead to not re-run tests

[no important files changed]
  • Loading branch information
huynhloc-1110 authored Aug 14, 2024
1 parent 7945fce commit f3c95cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
11 changes: 9 additions & 2 deletions exercises/practice/ledger/.meta/Example.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ let currencySymbol =
| "EUR" -> ""
| _ -> failwith "Invalid currency"

let currencyNegativePattern =
function
| "en-US" -> 0
| "nl-NL" -> 12
| _ -> failwith "Invalid locale"

let cultureInfo (locale: string) =
match locale with
| "en-US" | "nl-NL" -> new CultureInfo(locale)
| "en-US" | "nl-NL" -> new CultureInfo(locale, false)
| _ -> failwith "Invalid locale"

let dateFormat =
Expand All @@ -27,6 +33,7 @@ let dateFormat =
let getCulture (currency: string) (locale: string) =
let mutable culture = cultureInfo locale
culture.NumberFormat.CurrencySymbol <- currencySymbol currency
culture.NumberFormat.CurrencyNegativePattern <- currencyNegativePattern locale
culture.DateTimeFormat.ShortDatePattern <- dateFormat locale
culture

Expand Down Expand Up @@ -63,4 +70,4 @@ let formatLedger currency locale entries =
let culture = getCulture currency locale
let header = printHeader culture
let lines = List.map (printEntry culture) (orderEntries entries)
header :: lines |> String.concat "\n"
header :: lines |> String.concat System.Environment.NewLine
18 changes: 9 additions & 9 deletions exercises/practice/ledger/Ledger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let formatLedger currency locale entries =

for x in List.sortBy (fun x -> x.dat, x.des, x.chg) entries do

res <- res + "\n"
res <- res + System.Environment.NewLine

if locale = "nl-NL" then
res <- res + x.dat.ToString("dd-MM-yyyy")
Expand All @@ -39,23 +39,23 @@ let formatLedger currency locale entries =
if c < 0.0 then
if locale = "nl-NL" then
if currency = "USD" then
res <- res + ("$ " + c.ToString("#,#0.00", new CultureInfo("nl-NL"))).PadLeft(13)
res <- res + ("$ " + c.ToString("#,#0.00", new CultureInfo("nl-NL", false))).PadLeft(13)
if currency = "EUR" then
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("nl-NL"))).PadLeft(13)
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("nl-NL", false))).PadLeft(13)
if locale = "en-US" then
if currency = "USD" then
res <- res + ("($" + c.ToString("#,#0.00", new CultureInfo("en-US")).Substring(1) + ")").PadLeft(13)
res <- res + ("($" + c.ToString("#,#0.00", new CultureInfo("en-US", false)).Substring(1) + ")").PadLeft(13)
if currency = "EUR" then
res <- res + ("(€" + c.ToString("#,#0.00", new CultureInfo("en-US")).Substring(1) + ")").PadLeft(13)
res <- res + ("(€" + c.ToString("#,#0.00", new CultureInfo("en-US", false)).Substring(1) + ")").PadLeft(13)
else
if locale = "nl-NL" then
if currency = "USD" then
res <- res + ("$ " + c.ToString("#,#0.00", new CultureInfo("nl-NL")) + " ").PadLeft(13)
res <- res + ("$ " + c.ToString("#,#0.00", new CultureInfo("nl-NL", false)) + " ").PadLeft(13)
if currency = "EUR" then
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("nl-NL")) + " ").PadLeft(13)
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("nl-NL", false)) + " ").PadLeft(13)
if locale = "en-US" then
if currency = "USD" then
res <- res + ("$" + c.ToString("#,#0.00", new CultureInfo("en-US")) + " ").PadLeft(13)
res <- res + ("$" + c.ToString("#,#0.00", new CultureInfo("en-US", false)) + " ").PadLeft(13)
if currency = "EUR" then
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("en-US")) + " ").PadLeft(13)
res <- res + ("" + c.ToString("#,#0.00", new CultureInfo("en-US", false)) + " ").PadLeft(13)
res

0 comments on commit f3c95cf

Please sign in to comment.