none
Increasing Int by 1% RRS feed

  • Question

  • Hi,

    I have a var as an Int that includes multiple calculations to be used as an Int number (not a double, etc.)
    and whatever the var equals, should be increased by 1% of that value.

    I've reached the following in my code, but I feel something wrong:

    int IncreaseBy = 0; // the number is only 1 ~ 10%
    
    var result = (int)(/* some numbers */
                              (/*also numbers*/ + IncreaseBy * 0.100);
    
    Console.WriteLine("Result: " + result.ToString());

    Yes, I know its probably working but it may not mean its correct,
    I want this to be with no doubles, and 100% correct.

    and what if the "IncreaseBy" is 0 ?,
    will it still increase ? because I don't want it to be increased if it was 0% by any chance.

    Thanks in advance.

    Wednesday, November 7, 2018 8:11 PM

All replies

  • Hello, 

    I want this to be with no doubles, and 100% correct.

    Ok. 

    You have original value equal "1" and you have your demand to be increased exactly by "2%".

    You have several options:

    - 0

    - 1

    - 2

    And there is nothing between 0 and 1. There also nothing between 1 and 2.


    Which result will be 100% correct?


    Sincerely, Highly skilled coding monkey.

    Wednesday, November 7, 2018 9:35 PM
  • Check this example:

    int number = 1234;

    int increaseBy = 1;

    int result = number + number * increaseBy / 100;

     

    Console.WriteLine( number );

    Console.WriteLine( result );

     


    Wednesday, November 7, 2018 9:40 PM
  • You have original value equal "1" and you have your demand to be increased exactly by "2%".

    You have several options:

    - 0

    - 1

    - 2

    Which result will be 100% correct?


    Andrey Belyakov
    It's 0, the least number I'm getting that can increase the result is any num higher than 50, cause of the doubles maybe.
    example, increasing by 2% the num 50 = 50, 51 = 52, its getting increased normally after 50.
    example2, increasing by 10% the num 9 = 9, 10 = 11.
    • Edited by Zuher Laith Wednesday, November 7, 2018 10:47 PM
    Wednesday, November 7, 2018 10:47 PM
  • Check this example:

    int number = 1234;

    int increaseBy = 1;

    int result = number + number * increaseBy / 100;

     

    Console.WriteLine( number );

    Console.WriteLine( result );


    Viorel_
    I think its not right by any chance,
    int number = 1000;
    int increaseBy = 1; // %
    
    int result = number * increaseBy / 100;
    
    Console.WriteLine("value: " + number);
    Console.WriteLine("result: " + result);
    
    /// OUTPUT
    value: 1000
    result: 10

    Wednesday, November 7, 2018 10:48 PM
  • It's 0

    My recommendation would be to start study of basic mathematics.

    Problem is - 1 + 2% is 1.02.

    In your understanding 1.02 are equal to 0.00.

    Sorry, this is not related with programming - it's simple basic mathematics.

    In programming side - 1.02 can't be represented in integer. It should be rounded. Rounding to integer could be done by adding 0.5 - it's would be 1.52 -  and dropping anything after dot => 1

    But you ask

    no doubles, and 100% correct.

    and 1.00 != 1.02. So, there no possibility that your requirements can be covered.

    -----

    There is one trick to get closer to what you want - you need to get your number multiplied by 100 and add your percentage. You will have a result a 100 bigger, but it will be "no doubles and 100% correct". But this definitely isn't what your teacher expect from you. 


    Sincerely, Highly skilled coding monkey.


    Wednesday, November 7, 2018 11:30 PM
  • You must pay more attention to code - yours have some differences from given example. 

    Sincerely, Highly skilled coding monkey.

    Wednesday, November 7, 2018 11:31 PM
  • Check this example:

    int number = 1234;

    int increaseBy = 1;

    int result = number + number * increaseBy / 100;

     

    Console.WriteLine( number );

    Console.WriteLine( result );


    Viorel_
    I think its not right by any chance,
    int number = 1000;
    int increaseBy = 1; // %
    
    int result = number * increaseBy / 100;
    
    Console.WriteLine("value: " + number);
    Console.WriteLine("result: " + result);
    
    /// OUTPUT
    value: 1000
    result: 10


    Hi Zuher Laith,

    Thank you for posting here.

    >> whatever the var equals, should be increased by 1% of that value.

    I think you misunderstand the code provided by Viorel_. If you mean you want to get the result which increased by 1% of that value, you could try the code below.

     int number = 1000;
                int increaseBy = 1; // %
    
                int result = number + number * increaseBy / 100;
    
                Console.WriteLine("value: " + number);
                Console.WriteLine("result: " + result);
    
                /// OUTPUT
                //value: 1000
                //result: 1010
                Console.ReadKey();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, December 4, 2018 8:26 AM
    Thursday, November 8, 2018 2:31 AM
    Moderator