Skip to content

Commit e8cbf70

Browse files
authored
Merge pull request #305 from LarytheLord/fix/profile-display-decimal-weight-244
fix(profile): preserve decimal weight in profile display
2 parents 96ddf4f + 383f150 commit e8cbf70

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

lib/features/profile/presentation/bloc/profile_bloc.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:opennutritracker/core/utils/locator.dart';
1313
import 'package:opennutritracker/features/diary/presentation/bloc/calendar_day_bloc.dart';
1414
import 'package:opennutritracker/features/diary/presentation/bloc/diary_bloc.dart';
1515
import 'package:opennutritracker/features/home/presentation/bloc/home_bloc.dart';
16+
import 'package:opennutritracker/features/profile/presentation/utils/profile_display_format.dart';
1617

1718
part 'profile_event.dart';
1819

@@ -95,10 +96,10 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
9596

9697
/// Returns the user's weight in kg or lbs based on the user's config
9798
String getDisplayWeight(UserEntity user, bool usesImperialUnits) {
98-
if (usesImperialUnits) {
99-
return UnitCalc.kgToLbs(user.weightKG).toStringAsFixed(0);
100-
} else {
101-
return user.weightKG.roundToDouble().toStringAsFixed(0);
102-
}
99+
final displayWeight = usesImperialUnits
100+
? UnitCalc.kgToLbs(user.weightKG)
101+
: user.weightKG;
102+
103+
return formatProfileWeight(displayWeight);
103104
}
104105
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
String formatProfileWeight(double weight) {
2+
final roundedToOneDecimal = double.parse(weight.toStringAsFixed(1));
3+
if (roundedToOneDecimal == roundedToOneDecimal.roundToDouble()) {
4+
return roundedToOneDecimal.toStringAsFixed(0);
5+
}
6+
return roundedToOneDecimal.toStringAsFixed(1);
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:opennutritracker/features/profile/presentation/utils/profile_display_format.dart';
3+
4+
void main() {
5+
group('formatProfileWeight', () {
6+
test('shows whole numbers without decimal suffix', () {
7+
expect(formatProfileWeight(80.0), '80');
8+
});
9+
10+
test('preserves one decimal place for fractional values', () {
11+
expect(formatProfileWeight(80.5), '80.5');
12+
});
13+
14+
test('normalizes floating-point noise around whole values', () {
15+
expect(formatProfileWeight(154.0000001), '154');
16+
});
17+
18+
test('rounds values to one decimal place', () {
19+
expect(formatProfileWeight(154.25), '154.3');
20+
});
21+
});
22+
}

0 commit comments

Comments
 (0)