Quantity Displayed vs. Quantity Saved

Sometimes when saving an item to the Buy screen the quantity saved is one less than the quantity displayed.

On the Add Item screen I’m displaying the floating point slider quantity as a single digit without the decimal values using StringFormat, automatically removing the decimal portion of the number and inadvertently “rounding up”. When I save the item to the database, I convert the floating point slider quantity to an integer.

As you can see in the screenshots below there are situations where I’m displaying one quantity but saving a different value.

Quantity-Displayed-vs-Quantity-Saved-1

 

To fix this in the AddItemPage’s code behind OnSaveClicked method I changed the line where a new item is created.

Item newItem = new Item(itemName, addToBuyList, (int)quantitySlider.Value);

To this, rounding up the slider’s quantity value.

int quantityRoundedUp = (int)Math.Round(quantitySlider.Value);
Item newItem = new Item(itemName, addToBuyList, quantityRoundedUp);

Making sure the when the new item is created the displayed quantity is saved to the database instead of the slider’s floating point value. In the screenshot below the Qty value is what the slider’s value while the Display value is what should be saved to the database.

Quantity-Displayed-vs-Quantity-Saved-2

Some helpful links:

This entry was posted in Side Projects, Software Development and tagged , , . Bookmark the permalink.