0

I'm not sure how to make this function return the float in two decimal places.

    float calcPrice(float a, float b, string x) {
        if (x == "X") return (a - b) * 7.5;
        else return (a - b) * 9.75;
    }
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
Michael Paulinus
  • 97
  • 1
  • 1
  • 9
  • 5
    You can't control the decimal places in a float. Are you asking how to *print* just 2 decimal places? – cigien Aug 18 '20 at 12:21
  • 3
    You don’t; floats don’t really have decimal places, being binary and that. Work with integers and multiply your amounts by 100 instead. – molbdnilo Aug 18 '20 at 12:23
  • 1
    Maybe you want an integer instead and operate in cents instead of dollars. Floating point types are imprecise for monetary problems. – drescherjm Aug 18 '20 at 12:23
  • 2
    @drescherjm And pocket the fraction ;-). – Peter - Reinstate Monica Aug 18 '20 at 12:24
  • @cigen no I dont wanna print to 2 decimals, I wanna return the value from that function to a variable – Michael Paulinus Aug 18 '20 at 12:27
  • 3
    You can not do exactly what you want with a float or a double its as simple as that. You will likely run into this: [https://stackoverflow.com/questions/588004/is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – drescherjm Aug 18 '20 at 12:28
  • 1
    Though you clearly state "no I dont wanna print to 2 decimals", I admit that I do not believe you. I get a strong impression that what you really want to do is to store a value which if ever printed appears with two decimal places. I am convinced that this is a https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem and furthermore that it will not be satisfactorily solveable, because of the things discussed in the link provided above by drescherjm. To convince me otherwise, please describe how you would test any solution provided in an answer to this question - without printing. – Yunnosch Aug 18 '20 at 12:35
  • 4
    people already mentioned that floats/doubles do not have something like "decimal places". It is advised to not use floating point numbers for monetary calculation anyway. You either use fixed point math or use a different solution. – Raildex Aug 18 '20 at 12:39
  • @Raildex Good point about the non-float currency. Please turn that into an answer (core: Use int for cents, instead of float for dollars.), or find a duplicate please. – Yunnosch Aug 18 '20 at 12:41
  • Multiply by 100, truncate, then divide by 100.0. – Thomas Matthews Aug 18 '20 at 16:34
  • @MichaelPaulinus How does `trunc(x*100.0)/100.0` not work for you? What more do you need? – chux - Reinstate Monica Aug 18 '20 at 18:20

1 Answers1

3

You can't "truncate" a float. A float does not have something like a "decimal place". what you type into your code is just convencience, the binary representation of floats is completely different: https://en.wikipedia.org/wiki/Single-precision_floating-point_format

If you want to display the float in any way, you can still format it with functions like printf (keep in mind that it is a string after the formatting, not a float).

printf("%.2f", foobar);    // prints out foobar with 2 digits

It is also advised to not use floats for monetary calculations. Floats become imprecise with big(ger) numbers due to their small size (even doubles and other floating point formats will eventually run out of precision). Not to mention that floats are prone to rounding errors (again due to their limited size). These errors accumulate quite quickly.

For monetary calculations, you can use fixed point math. With fixed point math you really do have a fixed number of decimal places and the implementation is similar to basic integer math. You simply have to take care of the carry.

See here for more info about fixed point math: Fixed Point Arithmetic in C Programming

Raildex
  • 3,406
  • 1
  • 18
  • 42