From 838fbe3cf55dfd3db22df3f4321a0aa66a7abb03 Mon Sep 17 00:00:00 2001 From: ITwrx Date: Wed, 26 Jun 2024 22:44:26 -0500 Subject: [PATCH] Upload files to '' --- watts_to_dollars.nim | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 watts_to_dollars.nim diff --git a/watts_to_dollars.nim b/watts_to_dollars.nim new file mode 100644 index 0000000..d8a4fcb --- /dev/null +++ b/watts_to_dollars.nim @@ -0,0 +1,54 @@ +#[Copyright 2024 ITwrx. +This file is part of WattstoDollars. +WattstoDollars is released under the GNU General Public License 3.0. +See COPYING or for details.]# + +import std/[strutils, math] +import termstyle + +proc vInputInt(inputValue: string, inputPrompt: string, inputName: string): int = + try: + let intInputValue = parseInt(inputValue) + return intInputValue + except CatchableError: + echo red "\N" & inputName & " must be a whole number.\N" + echo inputPrompt + return vInputInt(readLine(stdin), inputPrompt, inputName) + +proc vInputFloat(inputValue: string, inputPrompt: string, inputName: string): float = + try: + let floatInputValue = parseFloat(inputValue) + return floatInputValue + except CatchableError: + echo red "\N" & inputName & " must be a float/decimal number.\N" + echo inputPrompt + return vInputFloat(readLine(stdin), inputPrompt, inputName) + +echo yellow "\N**** Whole numbers required for all inputs except electrical rate, which accepts a float/decimal number. ***\N" +var prompt: string +#get watts from user +prompt = "Watts?\N" +echo prompt +echo yellow "Hint: You can enter the total watts used by a device, or enter the difference in watts between two devices, to see how much more a second device will cost.\N" +var watts = vInputInt(readLine(stdin), prompt, "Watts") +#get watt hrs from user. +prompt = "\NNumber of hours of usage per day?\N" +echo prompt +var hours = vInputInt(readLine(stdin), prompt, "Hours") +#get days from user. +prompt = "\NNumber of days of usage?\N" +echo prompt +var days = vInputInt(readLine(stdin), prompt, "Days") +#get energy rate from user. +prompt = "\NElectrical rate per kilowatt hour?\N" +echo prompt +var kwRate = vInputFloat(readLine(stdin), prompt, "Electrical Rate") +let watthrsPerDay: int = watts * hours +#convert watts to kw hrs +let kwh: float = watthrsPerDay / 1000 +#get kwhr usage per # of days. +let kwhUsage: float = kwh * parseFloat($days) +#get $ usage. +let energyCost: float = kwhUsage * parseFloat($kwRate) +let roundedEnergyCost = round(energyCost, 2) +echo "\N" & $watts & " watts, used " & $hours & " hours a day, for " & $days & " days = $" & $roundedEnergyCost & "\N"