删除双精度小数点后的字符

时间:2022-03-14 17:15:06

How can I remove the all the characters after the decimal point.

如何删除小数点后的所有字符。

Instead of 7.3456, I would just like 7.

而不是7.3456,我只想7。

This is what I do to get the number so far with decimal places.

这就是我到目前为止用小数位数来获取数字的方法。

[NSString stringWithFormat:@" %f : %f",(audioPlayer.currentTime),(audioPlayer.duration) ];

Many Thanks, -Code

非常感谢,-Code

9 个解决方案

#1


46  

You can specify what you want using format string :

您可以使用格式字符串指定所需内容:

[NSString stringWithFormat:@" %.0f : %.0f", (audioPlayer.currentTime),
                                            (audioPlayer.duration)];

#2


4  

floorf() is the function you're looking for.

floorf()是你正在寻找的功能。

#3


4  

If you want this for display, use an NSNumberFormatter:

如果要将其显示,请使用NSNumberFormatter:

double sevenpointthreefourfivesix = 7.3456;
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithDouble:sevenpointthreefourfivesix]]);

2011-12-20 20:19:48.813 NoDecimal[55110:903] 7

2011-12-20 20:19:48.813 NoDecimal [55110:903] 7

If you want a value without the fractional part, use round(). If you want the closest integer value not greater than the original value, use floor().

如果您想要一个没有小数部分的值,请使用round()。如果希望最接近的整数值不大于原始值,请使用floor()。

#4


3  

Cast to int:

转换为int:

[NSString stringWithFormat:@" %i : %i",(int)(audioPlayer.currentTime),(int)(audioPlayer.duration) ];

Casting like this always rounds down (eg: just removes everything after the decimal place). This is what you asked for.

这样的转换总是向下舍入(例如:只删除小数点后的所有内容)。这就是你要求的。

In the case of rounding to the NEAREST whole number you want to add 0.5 to the number

在舍入到NEAREST整数的情况下,您希望将0.5添加到该数字

[NSString stringWithFormat:@" %i : %i",(int)(audioPlayer.currentTime+0.5f),(int)(audioPlayer.duration+0.5f) ];

This will round to the nearest whole number. eg: 1.2 becomes 1.7 and casting to int makes 1. 3.6 becomes 4.1 and casting makes 4. :)

这将舍入到最接近的整数。例如:1.2变为1.7并且铸造到int使1. 3.6变为4.1并且铸造变为4. :)

#5


3  

you are after

你在追求

[NSString stringWithFormat:@" %.00f : %.00f",(audioPlayer.currentTime),(audioPlayer.duration) ];

When formatting float you can tell the precision by the number before the f

格式化浮动时,您可以通过f之前的数字来表示精度

#6


2  

Why not just cast the audioPlayer.currentTime to an integer before you use stringWithFormat?

为什么不在使用stringWithFormat之前将audioPlayer.currentTime强制转换为整数?

[NSString stringWithFormat:@"%d", (int)(audioPlayer.currentTime)];

#7


0  

All you need to do is type-cast the double to an int, like so: int currentTime_int = (int)audioPlayer.currentTime;.

您需要做的就是将double类型转换为int,如下所示:int currentTime_int =(int)audioPlayer.currentTime;。

You can use this same approach for the other variable.

您可以对另一个变量使用相同的方法。

#8


0  

Many of the shorter answers here will work correctly. But if you want your code to be really clear and readable, you might want to explicitly specify your desired conversion from float to int, such as using:

这里的许多较短的答案都能正常工作。但是如果您希望代码非常清晰和可读,您可能希望明确指定从float到int的所需转换,例如使用:

int tmpInt = floorf(myFloat);  // or roundf(), etc.

and then separately specifying how you want the integer formated, e.g.

然后单独指定您希望如何格式化整数,例如

... stringWithFormat:@"%d", tmpInt ...  // or @"%+03d", etc.

instead of assuming that an inline cast shows what you want.

而不是假设内联演员表明你想要的。

#9


0  

You may also use

你也可以使用

double newDvalue =floor(dValue);

double newDvalue = floor(dValue);

it will remove all the decimals point

它将删除所有小数点

using %.0f for string format will be good also

使用%.0f作为字符串格式也会很好

#1


46  

You can specify what you want using format string :

您可以使用格式字符串指定所需内容:

[NSString stringWithFormat:@" %.0f : %.0f", (audioPlayer.currentTime),
                                            (audioPlayer.duration)];

#2


4  

floorf() is the function you're looking for.

floorf()是你正在寻找的功能。

#3


4  

If you want this for display, use an NSNumberFormatter:

如果要将其显示,请使用NSNumberFormatter:

double sevenpointthreefourfivesix = 7.3456;
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithDouble:sevenpointthreefourfivesix]]);

2011-12-20 20:19:48.813 NoDecimal[55110:903] 7

2011-12-20 20:19:48.813 NoDecimal [55110:903] 7

If you want a value without the fractional part, use round(). If you want the closest integer value not greater than the original value, use floor().

如果您想要一个没有小数部分的值,请使用round()。如果希望最接近的整数值不大于原始值,请使用floor()。

#4


3  

Cast to int:

转换为int:

[NSString stringWithFormat:@" %i : %i",(int)(audioPlayer.currentTime),(int)(audioPlayer.duration) ];

Casting like this always rounds down (eg: just removes everything after the decimal place). This is what you asked for.

这样的转换总是向下舍入(例如:只删除小数点后的所有内容)。这就是你要求的。

In the case of rounding to the NEAREST whole number you want to add 0.5 to the number

在舍入到NEAREST整数的情况下,您希望将0.5添加到该数字

[NSString stringWithFormat:@" %i : %i",(int)(audioPlayer.currentTime+0.5f),(int)(audioPlayer.duration+0.5f) ];

This will round to the nearest whole number. eg: 1.2 becomes 1.7 and casting to int makes 1. 3.6 becomes 4.1 and casting makes 4. :)

这将舍入到最接近的整数。例如:1.2变为1.7并且铸造到int使1. 3.6变为4.1并且铸造变为4. :)

#5


3  

you are after

你在追求

[NSString stringWithFormat:@" %.00f : %.00f",(audioPlayer.currentTime),(audioPlayer.duration) ];

When formatting float you can tell the precision by the number before the f

格式化浮动时,您可以通过f之前的数字来表示精度

#6


2  

Why not just cast the audioPlayer.currentTime to an integer before you use stringWithFormat?

为什么不在使用stringWithFormat之前将audioPlayer.currentTime强制转换为整数?

[NSString stringWithFormat:@"%d", (int)(audioPlayer.currentTime)];

#7


0  

All you need to do is type-cast the double to an int, like so: int currentTime_int = (int)audioPlayer.currentTime;.

您需要做的就是将double类型转换为int,如下所示:int currentTime_int =(int)audioPlayer.currentTime;。

You can use this same approach for the other variable.

您可以对另一个变量使用相同的方法。

#8


0  

Many of the shorter answers here will work correctly. But if you want your code to be really clear and readable, you might want to explicitly specify your desired conversion from float to int, such as using:

这里的许多较短的答案都能正常工作。但是如果您希望代码非常清晰和可读,您可能希望明确指定从float到int的所需转换,例如使用:

int tmpInt = floorf(myFloat);  // or roundf(), etc.

and then separately specifying how you want the integer formated, e.g.

然后单独指定您希望如何格式化整数,例如

... stringWithFormat:@"%d", tmpInt ...  // or @"%+03d", etc.

instead of assuming that an inline cast shows what you want.

而不是假设内联演员表明你想要的。

#9


0  

You may also use

你也可以使用

double newDvalue =floor(dValue);

double newDvalue = floor(dValue);

it will remove all the decimals point

它将删除所有小数点

using %.0f for string format will be good also

使用%.0f作为字符串格式也会很好