Skip to content

Commit

Permalink
fix: round targetPrice to 2 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
lucafluri committed Jul 14, 2020
1 parent cf885f4 commit 936e8c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
8 changes: 1 addition & 7 deletions lib/models/product.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:price_tracker/services/parsers/abstract_parser.dart';
import 'package:price_tracker/services/product_utils.dart';
import 'package:price_tracker/services/scraper.dart';
import 'package:string_validator/string_validator.dart';

Expand Down Expand Up @@ -136,11 +135,6 @@ class Product {
: this.name;
}

double roundToPlace(double d, int places) {
double mod = pow(10.0, places);
return ((d * mod).round().toDouble() / mod);
}

bool priceFall() {
int length = prices.length;

Expand Down
9 changes: 6 additions & 3 deletions lib/screens/product_detail/product_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:price_tracker/models/product.dart';
import 'package:price_tracker/screens/home/home.dart';
import 'package:price_tracker/services/product_utils.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:timeago/timeago.dart' as timeago;

Expand Down Expand Up @@ -32,7 +33,7 @@ class _ProductDetailState extends State<ProductDetail> {

Product p = await _db.getProduct(widget.product.id);
setTarget = p.targetPrice >= 0;
canSetTarget = p.prices[p.prices.length - 1] > 0;
canSetTarget = p.latestPrice > 0;
if (setTarget) {
_targetInputController.text = p.targetPrice.toString();
validTarget = true;
Expand Down Expand Up @@ -212,8 +213,10 @@ class _ProductDetailState extends State<ProductDetail> {
await DatabaseService.getInstance();

if (validTarget) {
product.targetPrice =
double.parse(value).abs();
product.targetPrice = roundToPlace(
double.parse(value).abs(), 2);
_targetInputController.text =
product.targetPrice.toString();
await _db.update(product);
} else {
_targetInputController.text =
Expand Down
7 changes: 7 additions & 0 deletions lib/services/product_utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:price_tracker/models/product.dart';
import 'package:price_tracker/screens/product_detail/product_detail.dart';
import 'package:price_tracker/services/database.dart';
Expand All @@ -7,6 +9,11 @@ import 'package:price_tracker/services/notifications.dart';

double reloadProgress;

double roundToPlace(double d, int places) {
double mod = pow(10.0, places);
return ((d * mod).round().toDouble() / mod);
}

//Returns number of products that fell under the set target
Future<int> countFailedParsing() async {
final _db = await DatabaseService.getInstance();
Expand Down
9 changes: 5 additions & 4 deletions test/product_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:price_tracker/models/product.dart';
import 'package:price_tracker/services/product_utils.dart';

// Product Class Unit Tests
void main() {
Expand Down Expand Up @@ -150,16 +151,16 @@ void main() {

group('roundToPlace', () {
test('round int', () {
expect(p.roundToPlace(1, 2), 1);
expect(roundToPlace(1, 2), 1);
});
test('round double down', () {
expect(p.roundToPlace(1.1234, 2), 1.12);
expect(roundToPlace(1.1234, 2), 1.12);
});
test('round double ', () {
expect(p.roundToPlace(1.255, 2), 1.25);
expect(roundToPlace(1.255, 2), 1.25);
});
test('round double up', () {
expect(p.roundToPlace(1.256, 2), 1.26);
expect(roundToPlace(1.256, 2), 1.26);
});
});

Expand Down

0 comments on commit 936e8c4

Please sign in to comment.