0

I am trying to get my numbers as accurate as possible when the user enters the information for fuel purchased. I also would like it so in the example below a user can just enter "84" for the gallons.

  • Gallons = 84.000
  • Cost per gallon = 5.199
  • Total cost = 436.72

I have it so the numbers display correctly but can't figure out how to save it to firebase.

class AddFuelViewModel: ObservableObject {
    private let repo: RepositoryProtocol
    
    var cost: String = ""
    var total: String = ""
    var gallons: String = ""
    
    @Published var saved: Bool = false
    
    init(repo: RepositoryProtocol) {
        self.repo = repo
    }
    
    func addFuel() {
        let fuel = Fuel(cost: Decimal(Int(actualCost) ?? 0),
                        total: Decimal(Int(actualTotal) ?? 0),
                        gallons: Decimal(Int(gallons) ?? 0))
        
        repo.addFuel(fuel: fuel) { result in
            switch result {
            case .success(let savedFuel):
                DispatchQueue.main.async {
                    self.saved = savedFuel == nil ? false : true
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}

I know this isn't correct but haven't been able to find the correct way.

OEZ
  • 55
  • 8
  • I have seen the option of saving it as cents and then formatting it to display properly but that doesn't seem very user friendly. I am hoping someone has away of saving decimal numbers to firebase. – OEZ May 06 '22 at 18:12
  • Just use `Double` you don't need the conversions – lorem ipsum May 06 '22 at 21:45
  • @loremipsum all the research I have done shows that using double is not accurate. – OEZ May 06 '22 at 22:29
  • Not accurate in what way? What are your requirements? – Joakim Danielson May 07 '22 at 16:52
  • @JoakimDanielson I am wanting to be able to calculate profit and loss for a business and also let them calculate quarterly and yearly taxes. – OEZ May 07 '22 at 18:12
  • And how does accuracy come into play here, what is the problem? When I asked about requirements I meant for the accuracy/calculations. – Joakim Danielson May 07 '22 at 18:21
  • @JoakimDanielson I thought I explained that….. profit and loss and taxes should always be accurate. – OEZ May 08 '22 at 17:33
  • You are explaining the meaning of accurate with the word accurate, we are not making any progress here. Maybe it would help you to read [this question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) because maybe your idea of double not being “accurate” enough is the problem here. If you want 2 or 3 decimal precision then what does it matter if the 8 or 9 decimal is wrong? Just round the value before you use it. – Joakim Danielson May 08 '22 at 18:39
  • @JoakimDanielson I didn’t realize I had to define the word accurate…. If you are rounding then it’s not accurate…. As far as how far off I have no clue…. I just keep reading that double or floating numbers are not accurate and shouldn’t be used for currency. – OEZ May 08 '22 at 20:13
  • _“If you are rounding then it’s not accurate”_, if you have stored 1 as 0.9999999999999 and you round that to 3 decimal precision when reading the value you get 1.000. How is that not accurate? Store as double (or int) and then use Decimal in the code. – Joakim Danielson May 08 '22 at 21:06
  • @JoakimDanielson why do they say not to use a double or float for currency? I am trying to learn the correct way of doing things. – OEZ May 09 '22 at 00:54

0 Answers0