使用字符串操作截断整数?
Truncating integer using string manipulation?
我有一个 class 的数据成员,无论输入位数是多少,都需要四舍五入为 2 位整数。
例如:
roundUpto2digit(12356463) == 12
roundUpto2digit(12547984) == 13 // The 5 rounds the 12 up to 13.
目前我的代码如下所示:
int roundUpto2digit(int cents){
// convert cents to string
string truncatedValue = to_string(cents);
// take first two elements corresponding to the Most Sign. Bits
// convert char to int, by -'0', multiply the first by 10 and sum the second
int totalsum = int(truncatedValue[0]-'0')*10 + int(truncatedValue[1]-'0');
// if the third element greater the five, increment the sum by one
if (truncatedValue[2]>=5) totalsum++;
return totalsum;
}
任何让它不那么难看的建议都将不胜感激。
#include <math.h>
int roundUpto2digit(int value)
{
value /= pow(10,(int)log10(value)-2);
/*
(int)log10(value) returns base ten logarithm (number of digits of your number)
pow(10, N) returns number 1 followed by N zeros
example:
pow(10,(int)log10(123)-2) returns 1
pow(10,(int)log10(1234)-2) returns 10
pow(10,(int)log10(1234567)-2) returns 10000
thus
value / pow(10,(int)log10(value)-2) returns first 3 digits
*/
return value%10>=5? (value+10)/10 : value/10;
/*
if(value%10>=5)// the last digit is >= 5
{
// increase previous number
value = value + 10;
}
// return number without the last digit
return value/10;
*/
}
您可以使用定点整数算法,它可能更快并且看起来更好。您想要 10^2 范围内的数字,并且您也拥有 10 的任意比例幂,因此要四舍五入,您只需要应用公式:
ROUNDED_VAL = (INITIAL_VAL + (10^(ORIG_SCALE - 2) / 2)) / 10^(ORIG_SCALE - 2)
因此您的代码可能如下所示:
int roundUpto2digit(int cents){
int scale = 10;
while(cents / scale > 0) {
// Find out original scale. This can be done maybe faster with a divide and conquer algorithm
scale *= 10;
}
int applied_scale = scale / 100;
if (applied_scale == 0) {
// In case there is only one digit, scale it up to two
return 10 * cents;
}
return ((cents + (applied_scale / 2)) / applied_scale);
}
编辑:我写的 10 * cents
行是根据我的解释对我提出的问题的任意推断。如果这不是所需的行为,当然可以更改。
我有一个 class 的数据成员,无论输入位数是多少,都需要四舍五入为 2 位整数。
例如:
roundUpto2digit(12356463) == 12
roundUpto2digit(12547984) == 13 // The 5 rounds the 12 up to 13.
目前我的代码如下所示:
int roundUpto2digit(int cents){
// convert cents to string
string truncatedValue = to_string(cents);
// take first two elements corresponding to the Most Sign. Bits
// convert char to int, by -'0', multiply the first by 10 and sum the second
int totalsum = int(truncatedValue[0]-'0')*10 + int(truncatedValue[1]-'0');
// if the third element greater the five, increment the sum by one
if (truncatedValue[2]>=5) totalsum++;
return totalsum;
}
任何让它不那么难看的建议都将不胜感激。
#include <math.h>
int roundUpto2digit(int value)
{
value /= pow(10,(int)log10(value)-2);
/*
(int)log10(value) returns base ten logarithm (number of digits of your number)
pow(10, N) returns number 1 followed by N zeros
example:
pow(10,(int)log10(123)-2) returns 1
pow(10,(int)log10(1234)-2) returns 10
pow(10,(int)log10(1234567)-2) returns 10000
thus
value / pow(10,(int)log10(value)-2) returns first 3 digits
*/
return value%10>=5? (value+10)/10 : value/10;
/*
if(value%10>=5)// the last digit is >= 5
{
// increase previous number
value = value + 10;
}
// return number without the last digit
return value/10;
*/
}
您可以使用定点整数算法,它可能更快并且看起来更好。您想要 10^2 范围内的数字,并且您也拥有 10 的任意比例幂,因此要四舍五入,您只需要应用公式:
ROUNDED_VAL = (INITIAL_VAL + (10^(ORIG_SCALE - 2) / 2)) / 10^(ORIG_SCALE - 2)
因此您的代码可能如下所示:
int roundUpto2digit(int cents){
int scale = 10;
while(cents / scale > 0) {
// Find out original scale. This can be done maybe faster with a divide and conquer algorithm
scale *= 10;
}
int applied_scale = scale / 100;
if (applied_scale == 0) {
// In case there is only one digit, scale it up to two
return 10 * cents;
}
return ((cents + (applied_scale / 2)) / applied_scale);
}
编辑:我写的 10 * cents
行是根据我的解释对我提出的问题的任意推断。如果这不是所需的行为,当然可以更改。