question

METHUSPHONPRASIT-6288 avatar image
0 Votes"
METHUSPHONPRASIT-6288 asked JessieZhang-2116 commented

How to do math BMI calculator with databinding

I want to do BMI calculator. At the beginning it show weight 0 in Label, Height 0 in Label, Bmi NaN in Label i dont want to show number. How to fix it.

97949-untitled.png




BmiViewModel.cs
public class BmiViewModel : INotifyPropertyChanged
{
private double height ;
private double weight ;


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
     {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
     public double Height
     {
         get => height;
             set
         {
             height = value;
             RaisePropertyChanged(nameof(Bmi));
         }
     }
     public double Weight
     {
         get => weight;
         set
         {
             weight = value;
             RaisePropertyChanged(nameof(Bmi));
         }
     }
       

     public double Bmi
         => Math.Round(Weight / Math.Pow(Height /100, 2), 2);
 }

BmiPage.xaml

         <Label Text="BMI Calculator" Margin="20" TextColor="Black"  FontSize="30" HorizontalTextAlignment="Center" />
            
         <Entry Placeholder="Enter Weight" Text="{Binding Weight}"  />
         <Entry Placeholder="Enter Height" Text="{Binding Height}" />


          
         <Label Text="Your BMI" FontSize="30"/>
         <Label  FontSize="30"  Text="{Binding Bmi}"   />
dotnet-xamarin
untitled.png (33.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @METHUSPHONPRASIT-6288 , I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered

Hello,


Welcome to our Microsoft Q&A platform!

According to the Computational expression ,we can know that the Height shouldn't be value 0.

 Math.Round(Weight / Math.Pow(Height /100, 2), 2);

In this condition, you can verify the value of Height before calculating.If the Height value is 0,we can set the value to 0.

For example, you can do like this:

     public double Bmi
     {
         get {
             if (Height == 0) {
                 return 0;
             }
             else {
                 return Math.Round(Weight / Math.Pow(Height / 100, 2), 2);
             }
         }
     }

Best Regards,


Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.



Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.