Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbodner committed Nov 29, 2020
1 parent f4f02e9 commit a7ba6af
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package main

import (
"fmt"
"log"
"os"

"github.com/learning-go-book/formatter"
"github.com/shopspring/decimal"
)

func main() {
amount, _ := decimal.NewFromString(os.Args[1])
percent, _ := decimal.NewFromString(os.Args[2])
if len(os.Args) < 3 {
fmt.Println("Need two parameters: amount and percent")
os.Exit(1)
}
amount, err := decimal.NewFromString(os.Args[1])
if err != nil {
log.Fatal(err)
}
percent, err := decimal.NewFromString(os.Args[2])
if err != nil {
log.Fatal(err)
}
percent = percent.Div(decimal.NewFromInt(100))
total := amount.Add(amount.Mul(percent)).Round(2)
fmt.Println(formatter.Space(80, os.Args[1], os.Args[2], total.StringFixed(2)))
Expand Down

0 comments on commit a7ba6af

Please sign in to comment.