How do you add 25% to any value or variable in decimal format [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
How can I divide two integers to get a double?
5 answers
My code is here
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25 / 100) * quote;
}
if (coverageType == "full coverage")
quote += (50 / 100) * quote; ;
return quote;
I am basically trying to increase the value "quote" by 25% of itself if the user has a DUI. Please give me a simple solution of code that is possible.
c# percentage
marked as duplicate by DavidG
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 5 more comments
up vote
0
down vote
favorite
This question already has an answer here:
How can I divide two integers to get a double?
5 answers
My code is here
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25 / 100) * quote;
}
if (coverageType == "full coverage")
quote += (50 / 100) * quote; ;
return quote;
I am basically trying to increase the value "quote" by 25% of itself if the user has a DUI. Please give me a simple solution of code that is possible.
c# percentage
marked as duplicate by DavidG
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is the type ofquote
?
– Yeldar Kurmangaliyev
Nov 19 at 11:57
4
What's not working about this code?
– Mark
Nov 19 at 11:57
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
2
@JeffreyPadgett The problem in your code is that you actually do an integer division, so(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just doquote *= 1.25
.
– Yeldar Kurmangaliyev
Nov 19 at 11:58
1
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
How can I divide two integers to get a double?
5 answers
My code is here
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25 / 100) * quote;
}
if (coverageType == "full coverage")
quote += (50 / 100) * quote; ;
return quote;
I am basically trying to increase the value "quote" by 25% of itself if the user has a DUI. Please give me a simple solution of code that is possible.
c# percentage
This question already has an answer here:
How can I divide two integers to get a double?
5 answers
My code is here
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25 / 100) * quote;
}
if (coverageType == "full coverage")
quote += (50 / 100) * quote; ;
return quote;
I am basically trying to increase the value "quote" by 25% of itself if the user has a DUI. Please give me a simple solution of code that is possible.
This question already has an answer here:
How can I divide two integers to get a double?
5 answers
c# percentage
c# percentage
asked Nov 19 at 11:55
Jeffrey Padgett
11
11
marked as duplicate by DavidG
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by DavidG
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is the type ofquote
?
– Yeldar Kurmangaliyev
Nov 19 at 11:57
4
What's not working about this code?
– Mark
Nov 19 at 11:57
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
2
@JeffreyPadgett The problem in your code is that you actually do an integer division, so(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just doquote *= 1.25
.
– Yeldar Kurmangaliyev
Nov 19 at 11:58
1
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59
|
show 5 more comments
What is the type ofquote
?
– Yeldar Kurmangaliyev
Nov 19 at 11:57
4
What's not working about this code?
– Mark
Nov 19 at 11:57
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
2
@JeffreyPadgett The problem in your code is that you actually do an integer division, so(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just doquote *= 1.25
.
– Yeldar Kurmangaliyev
Nov 19 at 11:58
1
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59
What is the type of
quote
?– Yeldar Kurmangaliyev
Nov 19 at 11:57
What is the type of
quote
?– Yeldar Kurmangaliyev
Nov 19 at 11:57
4
4
What's not working about this code?
– Mark
Nov 19 at 11:57
What's not working about this code?
– Mark
Nov 19 at 11:57
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
2
2
@JeffreyPadgett The problem in your code is that you actually do an integer division, so
(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just do quote *= 1.25
.– Yeldar Kurmangaliyev
Nov 19 at 11:58
@JeffreyPadgett The problem in your code is that you actually do an integer division, so
(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just do quote *= 1.25
.– Yeldar Kurmangaliyev
Nov 19 at 11:58
1
1
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59
|
show 5 more comments
3 Answers
3
active
oldest
votes
up vote
3
down vote
25 / 100
does integer division so the result is 0
(of type int
). Make them decimal i.e. 25.0M / 100.0M
or better yet:
quote *= 1.25M;
add a comment |
up vote
0
down vote
The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
add a comment |
up vote
0
down vote
To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
quote *= 1.25M;
if (coverageType == "full coverage")
quote *= 1.5M;
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
25 / 100
does integer division so the result is 0
(of type int
). Make them decimal i.e. 25.0M / 100.0M
or better yet:
quote *= 1.25M;
add a comment |
up vote
3
down vote
25 / 100
does integer division so the result is 0
(of type int
). Make them decimal i.e. 25.0M / 100.0M
or better yet:
quote *= 1.25M;
add a comment |
up vote
3
down vote
up vote
3
down vote
25 / 100
does integer division so the result is 0
(of type int
). Make them decimal i.e. 25.0M / 100.0M
or better yet:
quote *= 1.25M;
25 / 100
does integer division so the result is 0
(of type int
). Make them decimal i.e. 25.0M / 100.0M
or better yet:
quote *= 1.25M;
answered Nov 19 at 11:59
bolov
30.1k668128
30.1k668128
add a comment |
add a comment |
up vote
0
down vote
The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
add a comment |
up vote
0
down vote
The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).
The problem is: the result of 25 / 100
or 50 / 100
is 0
, in C# and similar languages (C, C++, ...).
Why?
In C#, when you divide an integer (int
, long
, etc.) by an integer, the decimal section is truncated - 0.25
becomes 0
.
So, there are some solutions:
1) Simply write 0.25
(or 0.5
) in your code:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;
2) Convert one of the numebrs to double
or to float
, by attaching it the postfix D
(for double
) or F
(for float
), or by adding to it an .0
at the end (which does it a double
), or by casting it with (<type>)
:
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;
(You also can convert it to decimal
, explicitly or by attaching M
as postfix).
answered Nov 19 at 12:01
Chayim Friedman
94639
94639
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
add a comment |
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
Thank you so much. I appreciate your help very much. This is a perfect explanation.
– Jeffrey Padgett
Nov 19 at 12:07
add a comment |
up vote
0
down vote
To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
quote *= 1.25M;
if (coverageType == "full coverage")
quote *= 1.5M;
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
add a comment |
up vote
0
down vote
To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
quote *= 1.25M;
if (coverageType == "full coverage")
quote *= 1.5M;
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
add a comment |
up vote
0
down vote
up vote
0
down vote
To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
quote *= 1.25M;
if (coverageType == "full coverage")
quote *= 1.5M;
To add 25% to the cyrrent value so it will become 125% of the current value, so multipy it by 1.25 (125%):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
quote *= 1.25M;
if (coverageType == "full coverage")
quote *= 1.5M;
edited Nov 19 at 12:06
answered Nov 19 at 11:59
Ashkan Mobayen Khiabani
19.6k1564114
19.6k1564114
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
add a comment |
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
"quote" is a decimal variable, so needs multiplying by 1.25M & 1.5M otherwise you get a compile time error (CS0019 Operator '*=' cannot be applied to operands of type 'decimal' and 'double')
– PaulF
Nov 19 at 12:05
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
@pau you are right, thanks.
– Ashkan Mobayen Khiabani
Nov 19 at 12:06
add a comment |
What is the type of
quote
?– Yeldar Kurmangaliyev
Nov 19 at 11:57
4
What's not working about this code?
– Mark
Nov 19 at 11:57
A decimal type, it is a variable of type decimal.
– Jeffrey Padgett
Nov 19 at 11:58
2
@JeffreyPadgett The problem in your code is that you actually do an integer division, so
(25 / 100)
results to 0, and the whole sentence on the right side results to 0. Just doquote *= 1.25
.– Yeldar Kurmangaliyev
Nov 19 at 11:58
1
quote += (25M / 100M) * quote;
– Reniuz
Nov 19 at 11:59