首页 \ 问答 \ 数学计算 - 浮点数[重复](Math Calculation - float numbers [duplicate])

数学计算 - 浮点数[重复](Math Calculation - float numbers [duplicate])



float years = (1/31536000) * 883102.00;

我得到年= 0000000



This question already has an answer here:

I am doing the following

float years = (1/31536000) * 883102.00;

and I get years = 0000000

while the actual answer is 0.0.28

Any suggestion on what might be going wrong ?

更新时间:2019-12-13 08:11



float years = (1.0/31536000.0) * 883102.00;


float years = (1.0/31536000) * 883102.00;

可能也会有效。 您的第一个数字被视为整数。

You should instead do

float years = (1.0/31536000.0) * 883102.00;


float years = (1.0/31536000) * 883102.00;

might work as well. Your very first number is treated as integer.




正则表达式,我想在子匹配中返回(n)值(regex, I want to return (n) values in a submatch)

是的,捕获组通常只存储其模式的最后一次出现。 您可以将所有参数包装在一个组中以获取它们: \{\{([\w\-]+):\s*((?:\[[\w\%]+\][,\s]*)*)\}\} ^ ^ ^ ^ 然后用/[,\s]+/拆分,得到一个列表。 Yes, capturing groups usually store only the last occurrence of their pattern. You can wrap al

使用Java将数字转换为字符串(Convert digits to string with Java)

让我们假设你得到了数字301301301.在书面文字中这将是: 三百一百三十一数千三百 如您所见,您可以传递3位数,并始终获得相同的结果。 现在只需检查,如果你有9位数,然后一次只传递3位数字,并将你的结果字符串连接到百万和千位。 这是一些伪代码: final int number = 301301301; final int[] numbers = splitNumberInThree(301301301); final String textNumber = numbersToText(nu

无法在googlesheets中找到CopyTo(Cant find CopyTo in googlesheets)

getValues()只返回一个二维数组。 copyTo适用于范围对象,可以通过可选的options参数处理这些是可自定义的方法。 function CopyPaste() { var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('S

如何在多个线程中使用相同的类并返回一个变量(How to use the same class accross multiple threads and return a variable)

事实上,解决方案是Synclock。 我的问题是我锁定了错误的对象objMyClass,而不是当前的我和我没有使用Monitor.PulseAll()。 此外,我切换到使用ThreadPool.QueueUserWorkItem(AddressOf objMyClass,args),并在线程完成时引发的自定义事件上使用了SyncLock。 它更容易! 谢谢!! The solution was, in fact, Synclock. My issue was that I was locking

在r中引用增量变量(Referencing incremental variables in r)

我们可以尝试用mget来获取list输出 res <- do.call(cbind, mget(ls(pattern = "(A|B|C)\\d+"))) 假设我们需要单独计算“Aamax”,“Bbmax”等。 lstA <- mget(ls(pattern = "A\\d+")) lstB <- mget(ls(pattern = "B\\d+")) lstC <- mget(ls(pattern = "C\\d+")) 目前尚不清楚“Aamax”,“Bbmax”等。 We can try

如何以动态宽度,第一个左,第二个中心,第三个右边水平居中3个div元素?(How to horizontally center 3 div elements with a dynamic width, first left, second center, third right?)

只需设置每个元素的宽度33%,请参阅jsfiddle demo http://jsfiddle.net/4VaRD/2/ HTML <div class="container"> <div class="left">left</div> <div class="center">center</div> <div class="right">right</div> </div> CSS .container{ width: 100%; } .left{ background-color: red

CSS将元素居中并将另一个元素浮动到它的右侧?(CSS centering an element and floating another element to the right of it?)

一个基本的方法是这样的: article {margin: 0 200px;} #info {width: 190px;} 这使得居中区域不受侧边栏项目的影响。 A basic approach is something like this: article {margin: 0 200px;} #info {width: 190px;} That keeps the centered area out of the way of the sidebar items.

使用html表单将Javascript减法并乘以两位小数(Javascript subtraction and multiplication to two decimal places using html forms)

var answer = Number(currentprice) - Number(ourprice); 由于currentprice和ourprice是字符串,因此您需要将其解析为int或float或Number 。 您可以根据优先级使用parseInt或parseFloat函数。 如果您想要修复两个小数位,请尝试以下代码。 var answer = (Number(currentprice) - Number(ourprice)).toFixed(2); var annualvolu




  • NumPy标量,广播和__iadd__方法(NumPy scalars, broadcasting and the __iadd__ method)
  • 快速排序算法输出错误(Getting wrong output for quicksort algorithm)
  • Rails管理员:没有Rails 4应用程序的编辑,显示或删除按钮(Rails Admin: No edit, show, or delete buttons for Rails 4 app)
  • 部分刷新问题触发两次(Issue with partial refresh triggered twice)
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • EF6.1 beta 1如何获得Tooling Consolidation功能?(EF6.1 beta 1 how to get the Tooling Consolidation feature?)
  • 适用于Windows和PHP 5.5的Phalcon(Phalcon for Windows and PHP 5.5)
  • 有没有办法忽略hg责任中的提交?(Is there a way to ignore a commit in hg blame?)
  • Desire2通过API学习访问学期的权限(Desire2Learn Permissions to Access Semesters Via API)
  • 从aspx将服务器的文件复制到另一台服务器(Copy a file of a server to another server from aspx)
  • 使用YSlow / Web Developer扩展程序确定页面大小(Figuring out page size with YSlow / Web Developer extension)
  • Select语句将字符串中的前7个字符与其他表中的其他7个字符进行比较(Select statement which compares first 7 chars in a string with other 7 chars in other table)
  • 检测对注册表的更改,防止更改等(Detect changes to registry, prevent changes, etc)
  • 用户的鼠标/键盘运动分析是否有特殊名称?(Does the analysis of mouse/keyboard motions of a user have a special name?)
  • 获取Rails中资源的路由列表(Get list of routes for a resource in Rails)
  • 用函数值除以数字(Divide a function value by a number)
  • 熊猫Python:如何从今天开始创建失效列?(Pandas Python: How to create lapse since today column?)
  • GDK Bug报告(GDK Bug Reports)
  • 困惑于在python中循环遍历列表(confused about looping through lists in python)
  • 'less'由'which'输出指定的文件('less' the file specified by the output of 'which')
  • 如何在ASP.NET中优化AJAX(How to optimize AJAX in ASP.NET)
  • 在文本文件中转换纪元时间(Convert epoch times in text file)
  • 表细胞伸展(Table cell stretching)
  • GridView控件(GridView Control)
  • 从构建定义文件控制pdb文件输出(Control pdb file output from build defintion file)
  • *将方法传递给类(*Passing a Method into a Class)
  • 如何在HttpReceiveHttpRequest()中使用HTTPS?(How to use HTTPS with HttpReceiveHttpRequest()?)