我需要一個數的小數部分轉換成整數沒有逗號, ,比如我有3.35我要得到的只是35部分無零或逗號,C++數字的小數部分轉換成整數
因爲我使用了modf()函數來提取小數部分,但是如果有任何方法可以做到這一點或過濾'0',它會給我一個0.35 。部分我將非常感激,如果你告訴我如何使用較小的代碼,
我需要一個數的小數部分轉換成整數沒有逗號, ,比如我有3.35我要得到的只是35部分無零或逗號,C++數字的小數部分轉換成整數
因爲我使用了modf()函數來提取小數部分,但是如果有任何方法可以做到這一點或過濾'0',它會給我一個0.35 。部分我將非常感激,如果你告訴我如何使用較小的代碼,
以下是我將如何做到這一點。
#include <sstream>
#include <string>
int main()
{
double d = yourDesiredNumber; //this is your number
std::ostringstream out;
out << setprecision(yourDesiredPrecision) << std::fixed
<< std::showpoint << d;
std::istringstream in(out.str());
std::string wholePart; //you won't need this.
int fractionalPart;
std::getline(in, wholePart, '.');
in >> fractionalPart;
//now fractionalPart contains your desired value.
}
我敢肯定,而不是兩個不同的istringstream
和ostringstream
對象,你可能只用一個stringstream
對象僥倖,但我不知道的細節(從未使用過類),所以我沒」在示例中使用它。
我不會這樣做。這是緩慢和有損... –
@ RichardJ.RossIII:我誠實地等待你發表一個答案,看看你*會做什麼。 –
這讓我想起以下引用 - http://bash.org/?946461 – 2012-10-09 13:52:32
另一種解決方案
int precision= 100;
double number = 3.35;
int f = floor(xx);
double temp = (f - number) * -1;
int fractional_part = temp * precision;
'數字 - f'而不是'(f - 數字)* -1'怎麼樣? –
對於負數,這不起作用。 -0.1給出90 – digitalvision
如果你需要它作爲一個字符串,一個很簡單的空調風格的解決辦法是(應該適用於小數的變量數):
double yourNumber = 0.35f;
char buffer[32];
snprintf(buffer, 32, "%g", yourNumber);
strtok(buffer, "."); // Here we would get the part before . , should still check
char* fraction = strtok(NULL, ".");
int fractionAsInt = atoi(fraction);
這個例子缺乏在字符串錯誤的情況下進行錯誤處理,如果您只需要固定的小數位數,則不可行,因爲算術方法在那裏工作得更好。
比轉換成字符串,然後再返回位更高效:
int fractional_part_as_int(double number, int number_of_decimal_places) {
double dummy;
double frac = modf(number,&dummy);
return round(frac*pow(10,number_of_decimal_places));
}
像這樣的東西應該工作:通過使用戶
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static int get_frac(double value, unsigned short precision)
{
return (int)((value - (long)value) * pow(10, precision));
}
static int get_frac_no_trailing_zeros(double value, unsigned short precision)
{
int v = get_frac(value, precision);
while (v % 10 == 0)
v /= 10;
return v;
}
int main(int argc, char *argv[])
{
double v;
v = 123.4564;
printf("%.4f = %d\n", v, get_frac(v, 2));
printf("%.4f = %d\n", v, get_frac(v, 4));
printf("%.4f = %d\n", v, get_frac(v, 6));
printf("%.4f = %d\n", v, get_frac_no_trailing_zeros(v, 6));
return EXIT_SUCCESS;
}
你也可以想要麼避免調用pow
首先提供10的冪數,或使用查找表。
使用一些魔法STL,here is示例代碼:
typedef std::pair<int, int> SplitFloat;
SplitFloat Split(float value, int precision)
{
// Get integer part.
float left = std::floor(value);
// Get decimal part.
float right = (value - left) * float(std::pow(10, precision));
return SplitFloat(left, right);
}
可以改善,但是是非常簡單的。
#include <iostream>
#include <cmath>
double round(double r) {
return (r > 0.0) ? std::floor(r + 0.5) : std::ceil(r - 0.5);
}
double floor_to_zero(double f) {
return (f > 0.0) ? std::floor(f) : std::ceil(f);
}
double sign(double s) {
return (s < 0.0) ? -1.0 : 1.0;
}
int frac(double f, int prec) {
return round((f - floor_to_zero(f)) * prec) * sign(f);
}
int main() {
double a = 1.2345;
double b = -34.567;
std::cout << frac(a, 100) << " " << frac(b, 100) << std::endl; // 23 57
}
我只是做了一些接近你想要做的事情,雖然我還是很新的。無論如何,也許這將有助於未來的人,因爲我登陸這裏尋找我的問題的結果。
第一步是確保包含3.35的變量是雙精度,但這可能很明顯。
接下來,創建一個只有整數的變量,並將其值設置爲double的值。它將只包含整個數字。
然後從double中減去整數(int)。您將剩下分數/十進制值。從那裏開始,乘以100.
超過100ths的十進制值,你將不得不做更多的配置顯然,但它應該是一個相當簡單的做一個if語句。如果十進制值大於.99,則乘以1000等。
總是隻有兩位小數?如何乘以100並轉換爲整數? –
將其格式化爲一個字符串,然後將小數點後面的所有內容都複製出來,或刪除一切直到第一個數字字符。 (那麼你可以將它轉換回int,如果這就是你所要求的) – Mordachai
3.35的二進制表示很可能是'3.349999986f'左右。 3.350的二進制表示是相同的。考慮到兩個輸入3.35和3.350,你期望的輸出是什麼? – MSalters