int percent = (share->numowned*100)/corp->numshares;
int mills = (share->numowned*1000)/corp->numshares;
printf("%d.%d", mills/10, mills%10);
int mills = (share->numowned*1000)/corp->numshares;
printf("%d.%d", mills/10, mills%10);
float x = share->numowned;
float percent = (x/corp->numshares)*100;
float x = share->numowned;
float percent = (x/corp->numshares)*100;
printf("%.2f", percent);
float x = share->numowned;
float percent = (x/corp->numshares)*100;
printf("%.2f", percent);
float price = share->price;
float gross = (x/corp->numshares) * price
float net = gross - 1.10 // take off a 1.10 commission perhaps?
printf("%.2f", net); // gives the wrong answer
$ cat test.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
float price = 25.5;
float gross = 2 * price;
float net = gross - 1.10; // take off a 1.10 commission perhaps?
printf("%.2f\n", net); // gives the wrong answer
printf("%.2f\n", -1.0);
printf("%.2f\n", -10.0);
printf("%.2f\n", -100.0);
}
$ gcc test.c
$ ./a.out
49.90
-1.00
-10.00
-100.00
// Broken - uses floating point for monetary calculation!
public static void main(String[] args) {
double funds = 1.00;
int itemsBought = 0;
for (double price = .10; funds >= price; price += .10) {
funds -= price;
itemsBought++;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Change: $" + funds);
}