PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 13, 2022

[FIXED] How to get truncated-to-integer division in C#?

 August 13, 2022     .net, c#, decimal, floating-point     No comments   

Issue

The following code keeps producing 5ft 11.8in instead of 4ft 11.8in when I type in 152 cm. I thought it was the floating point inaccuracies at first so I tried using decimal but that also gives me the same value. Is there anyway to make it more accurate? The number in height before it is divided by 12 is 59.84251....

class Height
{
    private decimal height;
    private string unit;
    public static string ConvertedHeight(decimal height, string unit)
    {
        if (unit == "m")
        {
            height = height * 100 / 2.54m;
            string conversion = (height / 12).ToString("F0") + "ft " + (height % 12).ToString("F1") + "in/n";
            return conversion;
        }
        else if (unit == "cm")
        {
            height = height / 2.54m;
            string conversion = (height / 12).ToString("F0") + "ft " + (height % 12).ToString("F1") + "in/n";
            return conversion;

Solution

You need cast your feet conversation to int in this case.

This is the code you need for division by 12.

(int) (height / 12)

Hope this helps!



Answered By - dj079
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing