Jump to content

VAT C code help

Go to solution Solved by C2dan88,

When you first set a with scanf that line should be

scanf("%f", &a);

Your code should be

#include <stdio.h>

int main()
{
    float a, sum;
    
    printf("\nEnter Value: $");
    scanf("%f", &a);
    
    sum = a * 0.175;
    printf("VAT: $%.2f", sum);
    
    sum = a - sum;
    printf("     New Value:  $%.2f", sum);
}

 

So i was told to write a code to calculate VAT , but it doesn't work as i want it to, for example when i put a value of 100 it should say 17.5 but instead of that it says 17, it doesn't show decimal places for some reason, if anyone could help that would be great . 

 

And also how do i so the function repeats again when you type R and exit when you click anything else .

 


 

Capture.PNG

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/
Share on other sites

Link to post
Share on other sites

You've declared both a and sum as integers which aren't decimal numbers. Not sure what the behaviour of C is when multiplying an int by a float, but it's probably the underlying cause of your problem.

 

Also the %d code in your printf statement is for integers, not floating point numbers.

 

[Edit] It seems that int * float will give a float. I think it's your print statement that's the issue. Try changing it to %f or something.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558186
Share on other sites

Link to post
Share on other sites

Also you need to change the %d's in printf and scanf since %d will only read/display integers as well. 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558193
Share on other sites

Link to post
Share on other sites

6 minutes ago, Helomegussta said:

when i put a value of 100 it should say 17.5 but instead of that it says 17

That's because you're using an int.

int is a variable type that holds a number, but only whole numbers, not numbers with decimal points. Because you're working with money, you obviously do want those decimal point, so change the variables that need a decimal point to a float/double.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558202
Share on other sites

Link to post
Share on other sites

4 minutes ago, Helomegussta said:

so what do i need to do , do i just change all int's to float or how does it work?

Probably all you need to do is replace your %d with %f (as I discovered above, float * int will become float):

https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

 

For good measure, however, since you are dealing with floating point numbers it would be "better" code to also declare them as a floating point type.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558203
Share on other sites

Link to post
Share on other sites

C will do implicit conversion between types

 

To print a float value youll need to %f of some sort, depending on how many values you want to output.

 

Eg.

printf("%.2f", floatVal);

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558205
Share on other sites

Link to post
Share on other sites

4 minutes ago, tikker said:

Probably all you need to do is replace your %d with %f:

https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

 

For good measure, however, since you are dealing with floating point numbers it would be "better" code to also declare them as a floating point type.

when i change %d to %f this is what am getting when i enter value 100 

Capture.PNG

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558210
Share on other sites

Link to post
Share on other sites

When you first set a with scanf that line should be

scanf("%f", &a);

Your code should be

#include <stdio.h>

int main()
{
    float a, sum;
    
    printf("\nEnter Value: $");
    scanf("%f", &a);
    
    sum = a * 0.175;
    printf("VAT: $%.2f", sum);
    
    sum = a - sum;
    printf("     New Value:  $%.2f", sum);
}

 

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558236
Share on other sites

Link to post
Share on other sites

12 minutes ago, Helomegussta said:

when i change %d to %.2f in scan and print statements am getting 0 for some reason.and int a,sum to float a,sum

Capture.PNG

What @C2dan88 says. The "1" you include in scanf seems to make it only read one character from a quick google (I don't really use C). Time to dive into C's documentation 😉

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558239
Share on other sites

Link to post
Share on other sites

2 minutes ago, C2dan88 said:

When you first set a with scanf that line should be


scanf("%f", &a);

Your code should be


#include <stdio.h>

int main()
{
    float a, sum;
    
    printf("\nEnter Value: $");
    scanf("%f", &a);
    
    sum = a * 0.175;
    printf("VAT: %.2f", sum);
    
    sum = a - sum;
    printf("     New Value:  $%.2f", sum);
}

 

so i didn't have to write .2f at the scanf("%ld", &a)

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558242
Share on other sites

Link to post
Share on other sites

4 minutes ago, C2dan88 said:

When you first set a with scanf that line should be


scanf("%f", &a);

Your code should be


#include <stdio.h>

int main()
{
    float a, sum;
    
    printf("\nEnter Value: $");
    scanf("%f", &a);
    
    sum = a * 0.175;
    printf("VAT: $%.2f", sum);
    
    sum = a - sum;
    printf("     New Value:  $%.2f", sum);
}

 

but thank you very much , i been trying to figure this out for 8 hours now

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558244
Share on other sites

Link to post
Share on other sites

6 hours ago, Helomegussta said:

but thank you very much , i been trying to figure this out for 8 hours now

It's a good learning experience.

 

A thing that no one has noted though, it's always important to remember the pitfalls of using floats/doubles with currencies  (i.e. Be careful if you are using them in anything that might be mission critical/needs accurate values).  Floats do handle decimals, but they are based on a binary system (and are finite).

 

For floats/doubles you can only do decimals in the form of 1/(2^n)...e.g. 0.75 = 1/2 + 1/4 = 0.11(binary), 0.5625 = 1/2 + 1/16 = 0.1001

The problem is when you have something like 0.1 or 0.9

e.g. 0.9 = 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + .... but since you are limited in the amount of numbers, 0.9 becomes 0.8999999999

 

That is why you have to be careful around floating points because if you go 0.9 + 0.9 + 0.9 + .... (and do it many many times) you will eventually end up with a small amount off than what the true value would be).  For the purposes of learning, it doesn't make a difference really and I'd argue in a lot of situations it won't matter as much (like video games where you can't purchase the currency).

3735928559 - Beware of the dead beef

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14558928
Share on other sites

Link to post
Share on other sites

Be so careful when calculating VAT.  You've spent 8 hours, but I've seen people spent several days on resolving issues in production as a result of VAT miscalulation.

 

The part where I've seen colleagues come unstuck is with rounding a value.  It is generally accepted that currency calculations should always round down (due to the fact that you cannot have a fraction of a penny in a decimalised currency system).  Assuming that your application is being written for a business and is non-trivial, check with your finances team how they calculate VAT.  Even better - get a decent sample of invoices you've processed on a previous system and run it through your algorithm to check it matches up.

 

If your business rounds down, make sure to round down after each calculation which may cause greater than two decimal places.  Here's an example where I've seen colleagues mess up:

  • Invoice X contains 2 units of Product Y at £9.99/unit
  • First, we calculate VAT => 9.99 + (9.99 * 0.2) = 11.988

However, we sold two units, so we should multiply this by two, right?

  • 11.988 * 2 = 23.976

If we just round this down to £23.97, depending on how your business handles VAT, you're now £0.01 over.  If you use standard mathematical rounding rules, rounding up to £23.98, you're now £0.02 over.

 

So let's go back and round that inc VAT value before we multiply by two...

  • 11.988 ≈ 11.99 (standard mathematical rounding rules)
  • 11.99 * 2 = 23.98

We're still £0.02 over.  This is because we should have rounded down - going against mathematical rules.  If we do that instead...

  • 11.988 ≈ 11.98 (overriding mathematical rounding rules)
  • 11.98 * 2 = 23.96

Now we have the inc VAT value we expected.

 

It may seem insignificant when we're speaking about this amount of money, but in our scenario the application was processing hundreds of invoices each day using this flawed logic.  Luckily, our accountancy software calculated VAT independently, so we were able to pick up on it when the inbounds funds didn't match rather than when we filed our taxes.

 

VAT is really confusing, especially when you're learning.  If you can offload your VAT calculation onto a system which has been tried and tested, I'd recommend it.

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14569727
Share on other sites

Link to post
Share on other sites

7 hours ago, empireOS said:
  • Invoice X contains 2 units of Product Y at £9.99/unit
  • First, we calculate VAT => 9.99 + (9.99 * 0.2) = 11.988

Isn't it rather that financial software would usually add all the "without tax" prices first, round if needed, then take the 0.2 of that?

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14570325
Share on other sites

Link to post
Share on other sites

On 3/18/2021 at 8:36 AM, Kilrah said:

Isn't it rather that financial software would usually add all the "without tax" prices first, round if needed, then take the 0.2 of that?

That’s another way of doing it, sure.  I don’t think there’s any set way of doing it per-se, I’d just like to express caution that it can be a very tricky business and is easy to get wrong.

Link to comment
https://linustechtips.com/topic/1314977-vat-c-code-help/#findComment-14576873
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×