< faqentry>如何将数字转换为精确到2位小数的字符串?< / faqentry> [英] <faqentry>How do I convert a Number into a String with exactly 2 decimal places?</faqentry>

查看:74
本文介绍了< faqentry>如何将数字转换为精确到2位小数的字符串?< / faqentry>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[原始帖子

< http://groups.google.com/group/comp....se_frm/thread/

b495b4898808fde0>

是超过一个月 - 这可能会导致在某些新闻服务器上张贴

的问题。这就是为什么我要开始一个新的]


我还是想完成这个圆形的混乱。作为一个启动引理,我们可以认为VK是所有时间和地点中最差的程序员:让我们来自这里的
请从这里向前移动。


任何程序的可用性取决于确切的行为描述。

这种方式必须知道i)我们可以提供什么参数和

ii)预期的结果。


当前FAQ代码在这方面的可用性为零。 取x / b $ b的功率,提取y和类似的不是行为描述 - 它

是形式算法的描述。它是相同的

遥控器手册提供了详细的电子架构

控件。显然,对其他一些正式算法的引用(符合ECMA-262部分......中的
)并没有增加任何可用性。

所以首先必须定义使用什么共同规则(行为)

进行四舍五入。


关键点是:

1 )如何绕损(当一些迹象必须被丢弃时)

2)如何圆形等分(十进制中它将是5)

3)如何圆形负面


随着里程碑值的进一步使用

1)1.033

2)1.035

3)-1.035

IEEE-754 / IEEE-754r为数字串定义五种舍入方法

表示:

(规范顺序改变了,实现的方法移动了

atop)


1)偏向最近,与零相关

由IE的toFixed方法实现。总体来说最着名的就是在学校学习



到固定(2)

1.033 = 1.03

1.035 = 1.04(一分为零,所以为正无穷大

为正值)

-1.035 = -1.04(一分为零,所以为负无穷大

为负值)


2)偏向零

由Gecko和Opera toFixed方法实现。

toFixed (2)

1.033 = 1.03

1.035 = 1.03(等值线条为零)

-1.035 = -1.03(等值线条为零)


3)偏向正无穷大

没有已知的ECMAScript实现(?)

toFixed(2)

1.033 = 1.03

1.035 = 1.04(与正无穷大相关联)

-1.035 = -1.03(与正无穷大有关)


4)偏向负无穷大

没有已知的ECMAScript实现(?)

toFixed(2)

1。 033 = 1.03

1.035 = 1.03(与负无穷大相关联)

-1.035 = -1.04(与负无穷大相关联)


就目前的IEEE-754而言。正如一些

海报所指出的那样,四舍五入不是一些自然的海报。来自数字的功能

自己。这仅仅是一种合同行为:让我们把它变成

这个...。这样,它可以是组合来自IEEE b
甚至所有单独的自定义算法的两个或更多规则的规则。在后一种情况下,

当然任何标准的称谓都是和错误修复必须是

掉线。在任何情况下都必须给出确切的行为描述。


这样我就不断要求解释什么是舍入规则

被FAQ维护者认为是正确的:

1)偏向最接近,从零开始(IE) - 因此Gecko

需要修复

2)偏向零(Gecko) ,Opera) - 因此IE需要修复

3)三分之一 - 因此每个人都需要修复

在第三种情况下,这个舍入规则是什么?

解决方案



VK在留言中写道

news:11 ******* ***************@q2g2000cwa.googlegro ups.com ...


[原始帖子

< http:// groups。 google.com/group/comp....se_frm/thread/

b495b4898808fde0>

超过一个月 - 这可能会对发布造成问题。这就是为什么我要开始一个新的]



是吗?或者更多的是你在那个帖子中发布的代码的问题

如此显然无能为力,你宁愿分散注意力,而b / b
形成它,以及它的危害如果你有任何开始的话,那将会对你的信誉产生影响。


我还是想完成这个圆形的混乱。



也许,但你已经证明它超出了你的理解范围。


作为一个创业引理我们可以认为VK是所有时间和地点中最差的

程序员:



根本不是程序员。程序员不会回应他们的代码中指出

错误,并指出他们不关心这种情况,正如你所做的那样。


让我们从这里向前移动。



这个诉求有什么意义?你远远落后于其他人

,你有几年的学习基础知识要在你面前学习

才有资格参与这个讨论。


< snip>


里程碑值随处可见

1)1.033

2)1.035

3)-1.035



< snip>


所以你还没有理解javascript''s数字类型不能

代表某些数字,所以它使用的数字是它可以代表的原始数字的最近近似值



理查德。





1月28日晚上11:30,理查德康福德" < Rich ... @ litotes.demon.co.uk>

写道:


< snip>里程碑值随处可见还有


1)1.033

2)1.035

3)-1.035< snip>



所以你还没有理解javascript'的数字类型不能

代表某些数字,所以相反它使用的数字是

最接近它可以代表的原始数字。



正如我曾经问过的那样:请讨厌,讽刺等等 - 但是

请不要傻。选择最接近的近似值。对于

这三个数字(当然可以是任何其他三元组)定义了使用中的

舍入机制。我拼写了IEEE-754中的所有四个,我解释了哪一个是由IE和Gecko选择的。

现在我要问四个中哪一个应该是_really_用于选择

人的意见。如果没有人,只有第五个,那么请拼写

它。如果IEEE官方条款提出太多挑战,那么简单地用b / b
给出舍入结果,用逗号后面的两个数字表示

被你认为是正确的:

correctToFixed(2)

1.033 =?

1.035 =?

-1.035 =?

这样会很容易显示你在

心中有哪些舍入方法。


VK写道:


Richard Cornford写道:


>< snip>随着里程碑值的进一步使用,


1)1.033

2)1.035

3)-1.035< snip>


所以你还没有理解javascript'的数字类型
不能代表某些数字,所以相反它使用的数字是最接近的数字

它可以代表的原始数字。



正如我曾经问过的那样:请讨厌,讽刺,所以

on - 但请不要傻。



无论你认为什么是愚蠢的,这都不关心我。你不需要理解这个主题,而不是做一些关于找出

的事情,而是在浪费时间来误导你自己。


选择最接近的近似值对于

这三个数字(当然可以是任何其他三元组)

定义了使用的舍入机制。



Bullshit。您没有意识到源代码中的数字文字

将导致数值类型的值(遵循ECMA 262指定

规则)但该数值不一定读取数字文字的文本可能是预期的数字。


使用的舍入是指定的,但不超过巧合

支持该语言执行的任何其他舍入。


我拼写了IEEE-754中的全部四个,


又一次你看错了地方,所以没有看到

的相关信息。


我解释了哪一个是由IE和Gecko选择的。



不,你没有。根据你的另一个误解,你所做的是关于你误解的内容。


现在我问的是哪一个应该按照选定的人的意见__
使用四个。



你的问题从错误的前提开始。 -toFixed -

实现的行为意味着除了实现的性质之外没有任何东西

的 - toFixed - 方法。


如果没有人,只有第五名,请拼写




Gibberish。


如果IEEE官方条款提出过多挑战

然后简单地用逗号后的两个数字给出舍入结果

被你认为是正确的:

correctToFixed(2)

1.033 = ?

1.035 =?

-1.035 =?



你问的是哪些结果 - toFixed - 方法的Number对象

会返回?有一个指定的算法 - toFixed - ,所以它的

正确输出是通过将算法

应用于输入而产生的。


另一方面,正如你多次被告知的那样,没有价值

javascript中的数字类型恰好代表1.035,所以

,值_can_not_是 - toFixed - 算法的输入值。


这将很容易显示你所采用的舍入方法

in你的想法。



不同的舍入方法适用于不同的应用程序。鉴于

,你的观念受到一些单一概念的限制

正确的学校四舍五入 (这本身对你来说显然太复杂了吗?你自己在javascript中实现
)你无法看到你坚持认为有一些绝对答案的无价值
/>
你的问题。


Richard。


[The original thread
<http://groups.google.com/group/comp....se_frm/thread/
b495b4898808fde0>
is more than one month old - this may pose problem for posting over
some news servers. This is why I''m starting a new one]

I''d still like to finish this rounding mess. As a startup lemma we can
take that VK is the worst programmer of all times and places: let''s
move from here forward please.

The usability of any program depends on exact behavior description.
This way it is a must to know i) what argument we can feed into and
ii) what results to expect.

The usability of the current FAQ code in this aspect is zero. "take
power of x, extract y" and similar is not a behavior description - it
is a description of the formal algorithm. It is the same as instead
remote control manual provide a detailed electronic schema of the
control. Obviously a reference to some other formal algorithm ("in
accordance with ECMA-262 section...") doesn''t add any usability.
So first it has to be defined what common rules (behavior) are used
for rounding.

The key points are:
1) how to round with losses (when some signs have to be dropped)
2) how to round equipoint (in decimal system it will be 5)
3) how to round negative

As milestone values used everywhere further are
1) 1.033
2) 1.035
3) -1.035

IEEE-754/IEEE-754r define five methods of rounding for numeral string
representation:
(the specification order is changed, the implemented methods moved
atop)

1) Biased round to nearest, ties away from zero
Implemented by IE''s toFixed method. Overall the most known as studied
at the school
toFixed(2)
1.033 = 1.03
1.035 = 1.04 (equipoint ties away zero, so to positive infinity
for positive values)
-1.035 =-1.04 (equipoint ties away zero, so to negative infinity
for negative values)

2) Biased towards zero
Implemented by Gecko and Opera toFixed method.
toFixed(2)
1.033 = 1.03
1.035 = 1.03 (equipoint ties towards zero)
-1.035 =-1.03 (equipoint ties towards zero)

3) Biased towards positive infinity
No known ECMAScript implementations (?)
toFixed(2)
1.033 = 1.03
1.035 = 1.04 (equipoint ties towards positive infinity)
-1.035 =-1.03 (equipoint ties towards positive infinity)

4) Biased towards negative infinity
No known ECMAScript implementations (?)
toFixed(2)
1.033 = 1.03
1.035 = 1.03 (equipoint ties towards negative infinity)
-1.035 =-1.04 (equipoint ties towards negative infinity)

This is as far as IEEE-754 currently goes. As it was pointed by some
posters, rounding is not some "natural" feature coming out of numbers
themselves. It is merely a contract behavior: "let''s make it as
this...". This way it can be a rule combining two or more rules from
IEEE or even all separate custom algorithm. In the latter case of
course any appellations to "standards" and "bug fixes" must be
dropped. In any case exact behavior description has to be given.

This way I am insistently asking to explain what rounding rule is
considered correct by FAQ maintainer:
1) Biased round to nearest, ties away from zero (IE) - thus Gecko
needs a fix
2) Biased towards zero (Gecko, Opera) - thus IE needs a fix
3) some third - thus everyone needs a fix
In the 3rd case what this rounding rule is?

解决方案


VK wrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...

[The original thread
<http://groups.google.com/group/comp....se_frm/thread/
b495b4898808fde0>
is more than one month old - this may pose problem for posting
over some news servers. This is why I''m starting a new one]

Is it? Or is it more a matter of the code you posted in that thread being
so self-evidently inept that you would rather distract attention away
form it, and the harm it would have done to your credibility, if you had
any to start with.

I''d still like to finish this rounding mess.

Maybe, but you have demonstrated that it is beyond your comprehension.

As a startup lemma we can take that VK is the worst
programmer of all times and places:

Not a programmer at all. A programmer would not response to having the
faults in their code pointed out by stating that they "don''t care about
that situation", as you did.

let''s move from here forward please.

What is the point of this appeal? You are so far behind everyone else
that you have a couple of years learning the basics to go before you
would become qualified to participate in this discussion.

<snip>

As milestone values used everywhere further are
1) 1.033
2) 1.035
3) -1.035

<snip>

So you have still not understood that javascript''s numeric type cannot
represent certain numbers, so instead it uses a number that is the
nearest approximation of the original number that it can represent.

Richard.




On Jan 28, 11:30 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

<snip>As milestone values used everywhere further are

1) 1.033
2) 1.035
3) -1.035<snip>


So you have still not understood that javascript''s numeric type cannot
represent certain numbers, so instead it uses a number that is the
nearest approximation of the original number that it can represent.

As I once asked prior: please be nasty, sarcastic and so on - but
please don''t be silly. The choice of the "nearest approximation" for
these three numbers (can be any other triplet of course) defines the
rounding mechanics in use. I spelled all four from IEEE-754, I
explained which one is chosen by IE and by Gecko.
Now I''m asking which one of four should be _really_ used by "selected
people"''s opinion. If no one but something fifth then please spell
it. If the IEEE official terms put too much of challenge then simply
give the rounding results with two numerals after comma which are
considered correct by you:
correctToFixed(2)
1.033 =?
1.035 =?
-1.035 =?
This would easily show what rounding method are you bearing in your
mind.


VK wrote:

Richard Cornford wrote:

><snip>As milestone values used everywhere further are

1) 1.033
2) 1.035
3) -1.035<snip>


So you have still not understood that javascript''s numeric type
cannot represent certain numbers, so instead it uses a number
that is the nearest approximation of the original number that
it can represent.


As I once asked prior: please be nasty, sarcastic and so
on - but please don''t be silly.

Whatever you may perceive as silly here is of no concern to me. You don''t
comprehend the subject and instead of doing something about finding out
you are wasting time with misdirecting yourself.

The choice of the "nearest approximation" for
these three numbers (can be any other triplet of course)
defines the rounding mechanics in use.

Bullshit. You are failing to grasp that a numeric literal in source code
will result in a value of numeric type (following ECMA 262 specified
rules) but that numeric value will not necessarily be the number that may
be expected from reading the numeric literal''s text.

The rounding used is specified, but has no more than a coincidental
bearing on any other rounding performed by the language.

I spelled all four from IEEE-754,

And again you were looking in the wrong place and so not seeing the
pertinent information.

I explained which one is chosen by IE and by Gecko.

No you did not. What you did was drivel on about what you misperceived
based upon another of your misconceptions.

Now I''m asking which one of four should be _really_ used
by "selected people"''s opinion.

Your question starts from a false premise. The behaviour of - toFixed -
implementations imply nothing beyond the nature of the implementations
of - toFixed - methods.

If no one but something fifth then please spell
it.

Gibberish.

If the IEEE official terms put too much of challenge
then simply give the rounding results with two numerals
after comma which are considered correct by you:
correctToFixed(2)
1.033 =?
1.035 =?
-1.035 =?

Are you asking which results the - toFixed - method of Number objects
would return? There is a specified algorithm for - toFixed -, so its
correct output is output that would be produced by applying the algorithm
to the input.

On the other hand, as you have repeatedly been told, there is no value of
the numeric type in javascript that precisely represents 1.035, and so
that value _can_not_ be the input value for the - toFixed - algorithm.

This would easily show what rounding method are you bearing
in your mind.

Differing rounding methods are suited to different applications. Given
that you are limited in your perceptions by some concept of a single
correct "school rounding" (that is itself apparently too complex for you
to implement in javascript yourself) you are incapable of seeing the
worthlessness of your insisting that there be some single absolute answer
to your question.

Richard.


这篇关于&lt; faqentry&gt;如何将数字转换为精确到2位小数的字符串?&lt; / faqentry&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆