Skip to content

Instantly share code, notes, and snippets.

@careo
Forked from criscokid/First Solution.c
Created November 19, 2010 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save careo/706986 to your computer and use it in GitHub Desktop.
Save careo/706986 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"4.0"];
NSDecimalNumber *paid = [NSDecimalNumber decimalNumberWithString:@"3.1"];
NSDecimalNumber *result = [price decimalNumberBySubtracting: paid];
//NSLog([result stringValue]);
NSLog(@"change = %@",[result stringValue]);
[pool drain];
return 0;
}
object SquareChange {
def main(args: Array[String]) {
val price = BigDecimal("4.0");
val paid = BigDecimal("3.1");
val change = price - paid;
println("change = " + change);
}
}
int main() {
int dollars, cents;
float paid = 4.00f;
float price = 3.10f;
int paidInt = paid * 100;
int priceInt = price * 100;
int changeInt = paidInt - priceInt;
dollars = (float)changeInt / 100;
cents = changeInt % 100;
printf("change = %d.%d\n", dollars, cents);
return 0;
}
/*
* If you pay $4.00 for a latte that costs
* $3.10, how much change do you get?
*/
int main() {
float change = 4.00f - 3.10f;
printf("change = %10.10f\n", change);
return 0;
}
// => 0.9000000954
#include <stdio.h>
int main() {
float floating = 4.00 - 3.10;
printf("float = %10.10f\n", floating);
double doubled = 4.00 - 3.10;
printf("double = %10.10f\n", doubled);
printf("float size = %zu\n", sizeof(floating));
printf("double size = %zu\n", sizeof(doubled));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment