首页 \ 问答 \ 如何检查浮子是否有两个有效数字?(How can I check if a float has two significant figures?)

如何检查浮子是否有两个有效数字?(How can I check if a float has two significant figures?)

我正在编写一个单元测试来确定字符串值是否出现2个有效数字,即。 “N.NN”

    strokeValue = [NSString stringWithFormat:@"%.2f",someFloatValue];

如何编写断言的测试,我的字符串总是有2个小数位?


I am writing a unit test to determine if a string value appears with 2 significant figures, ie. "N.NN"

    strokeValue = [NSString stringWithFormat:@"%.2f",someFloatValue];

How can I write a test that asserts, my string always has 2 decimal places?


原文:https://stackoverflow.com/questions/13220531
更新时间:2019-06-21 03:07

最满意答案

由于您使用%.2f格式说明符格式化浮点值,因此根据定义,结果字符串将始终具有两个小数位。 如果someFloatValue为5,你将获得5.00。 如果someFloatValue是3.1415926,你将获得3.14。

没有必要进行测试。 对于给定的格式说明符,它始终是真的。

编辑:我发现你可能确实想要确认你使用的是正确的格式说明符。 检查结果字符串的一种方法是:

NSRange range = [strokeValue rangeOfString:@"."];
assert(range.location != NSNotFound && range.location == strokeValue.length - 3, @"String doesn't have two decimals places");

Since you are formatting a float value using the %.2f format specifier, by definition, the resulting string will always have two decimal places. If someFloatValue is 5 you will get 5.00. If someFloatValue is 3.1415926 you will get 3.14.

There is no need to test. It will always be true with the given format specifier.

Edit: It occurs to me that you may actually want to confirm that in fact you are using the correct format specifier. One way to check the resulting string would be:

NSRange range = [strokeValue rangeOfString:@"."];
assert(range.location != NSNotFound && range.location == strokeValue.length - 3, @"String doesn't have two decimals places");
2012-11-04

相关问答

更多

如何强制两位数字留在LaTeX的同一页?(How to force two figures to stay on the same page in LaTeX?)

你可以把两个数字放在一个数字环境中。 例如: \begin{figure}[p] \centering \includegraphics{fig1} \caption{Caption 1} \includegraphics{fig2} \caption{Caption 2} \end{figure} 每个标题将生成一个单独的数字。 You can put two figures inside one figure environment. For example: \begin{figure}[ ...

如何检查两个不同的LINQ查询是否具有相同的结果?(How to check if two different LINQ queries has same result?)

如果这两个(或任何其他两个)语句是“相同的”,则定义非常差。 如果HasPrice不像人们期望的那样是什么? 在任何情况下,最接近答案的是你需要使用SMT算法来评估它。 我不知道你的用例存在“开箱即用”。 If these two (or any other two) statements are "the same" is very poorly defined. What if HasPrice is not a bool as one would expect? In any case, t ...

浮点数的最大值(maximum value in float)

在某些时候, x + 1.0f最接近的可表示值是x本身。 达到该点后,由于此舍入错误,您的循环不会导致sum进一步增加。 作为示例,您可以使用具有固定数量的有效数字的科学记数法来观察此效果。 例如,有4个有效数字: 0 = 0.000e0 1 = 1.000e0 2 = 2.000e0 3 = 3.000e0 ... 9 = 9.000e0 10 = 1.000e1 11 = 1.100e1 ... 99 = 9.900e1 100 = ...

在Z3中有效地检查两个约束的句法等价性(Checking syntactic equivalence of two constraints efficiently in Z3)

Expr类派生自AST,它具有用于此目的的equals , compareTo和hashCode函数。 由于Z3使用哈希引用,这是非常有效的,基本上只是比较指针(在Java中是一个类型)。 The Expr class derives from AST, which has equals, compareTo, and hashCode functions for this purpose. Since Z3 uses hash consing, this is very efficient, e ...

我如何在matplotlib中分别显示数字?(How can I show figures separately in matplotlib?)

当然。 使用add_subplot添加一个Axes 。 (编辑import 。)(编辑的show 。) import matplotlib.pyplot as plt f1 = plt.figure() f2 = plt.figure() ax1 = f1.add_subplot(111) ax1.plot(range(0,10)) ax2 = f2.add_subplot(111) ax2.plot(range(10,20)) plt.show() 或者,使用add_axes 。 ax1 = ...

我如何检查两个ArrayList是否有所不同,我不在乎发生了什么变化(How can I check if two ArrayList differ, I don't care what's changed)

下面是一个简单的方法,它检查2个数组列表是否包含相同的值,而不管它们的顺序如何 //the name of the method explains it well... public boolean isTwoArrayListsWithSameValues(ArrayList<Object> list1, ArrayList<Object> list2) { //null checking if(list1==null && list2==nul ...

如何检查两个对象的属性是否相等,而不破坏现有的Object.Equals()?(How do I check if two Objects are equal in terms of their properties only without breaking the existing Object.Equals()?)

你有没有尝试过实现你自己的IEqualityComparer? 您可以将它传递给.Equals()重载以定义您自己的自定义相等逻辑,如 用户A =用户B,即使它们是不同的实例,如果属性x,y,z是相同的。 看到这个: MSDN 编辑:我应该写你可以实例化你的EqualityComparer并将两个实例传递给它的Equals()方法并获得一个布尔值。 基本控制台应用程序...将显示真实,错误,错误。 事情是微不足道的,有两个属性显示。 var comparer = new ThingEquality ...

多个数字与rhtml和knitr(Multiple figures with rhtml and knitr)

绘图可以与gridExtra软件包结合使用。 如果您有三个图( p1 , p2和p3 ),则命令为: library(gridExtra) newPlot <- grid.arrange(p1, p2, p3) 查看gridExtra软件包以获取更多详细信息。 Plots can be combined with the gridExtra package. If you have, e.g., three plots (p1, p2, and p3), the command is: libr ...

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)