Floating point 将NSFileSystemize转换为GB
我需要将NSFileSystemize转换为GBFloating point 将NSFileSystemize转换为GB,floating-point,numbers,size,Floating Point,Numbers,Size,我需要将NSFileSystemize转换为GB NSDictionary * fsAttributes = [ [NSFileManager defaultManager] fileSystemAttributesAtPath:NSTemporaryDirectory()]; NSNumber *totalSize = [fsAttributes objectForKey:NSFileSystemSize]; NSString *sizeInGB = [NSString stri
NSDictionary * fsAttributes = [ [NSFileManager defaultManager]
fileSystemAttributesAtPath:NSTemporaryDirectory()];
NSNumber *totalSize = [fsAttributes objectForKey:NSFileSystemSize];
NSString *sizeInGB = [NSString stringWithFormat:@"\n\n %3.2f GB",[totalSize floatValue] / 107374824];
//returns 69.86 GB
你知道为什么它不能以8.0GB的速度返回吗?作为一个nit,
1024*1024*1024
是1073741824
,而不是107374824
(你在千位中缺少了一个1)。-(NSString*)格式化文件大小:(无符号长)大小
{
NSString*formattedStr=nil;
如果(大小==0)
formattedStr=@“空”;
其他的
如果(大小>0&&size<1024)
formattedStr=[NSString stringWithFormat:@“%qu字节”,大小];
其他的
如果(大小>=1024&&size=pow(1024,2)和大小=pow(1024,3))
formattedStr=[NSString stringWithFormat:@“%.3f GB”,(大小/功率(1024,3));
返回格式化的str;
}
抓得好。实际上,我使用的是:NSString*sizeInGB=[nsstringstringwithformat:@“\n\n%.2f GB”,[totalSize floatValue]/1024/1024];似乎工作正常。
- (NSString *)formattedFileSize:(unsigned long long)size
{
NSString *formattedStr = nil;
if (size == 0)
formattedStr = @"Empty";
else
if (size > 0 && size < 1024)
formattedStr = [NSString stringWithFormat:@"%qu bytes", size];
else
if (size >= 1024 && size < pow(1024, 2))
formattedStr = [NSString stringWithFormat:@"%.1f KB", (size / 1024.)];
else
if (size >= pow(1024, 2) && size < pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.2f MB", (size / pow(1024, 2))];
else
if (size >= pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.3f GB", (size / pow(1024, 3))];
return formattedStr;
}