JS retains two decimal places

Source: Internet
Author: User
Tags rounds
JS retains two decimal places JS retains two decimal places

 

For floating point numbers with multiple digits after decimal points, we may only need to retain two digits, but JS does not provide such a direct function. Therefore, we have to write the function to implement this function by ourselves. The Code is as follows:

Function changetwodecimal (X)
{
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Alert ('function: changetwodecimal-> parameter error ');
Return false;
}
F_x = math. Round (F_x* 100)/100;

Return f_x;
}

Function: rounds a floating point number to the second place after the decimal point.

Usage: changetwodecimal (3.1415926) returns 3.14

Changetwodecimal (3.1475926) returns 3.15

 

JS retains 2 decimal places (mandatory)

 

If the number of decimal places is greater than 2 digits, it is okay to use the above function, but if it is smaller than 2 digits, for example:

Changetwodecimal (3.1) will return 3.1. If you need a format like 3.10, you need the following function:

 

Function changetwodecimal_f (X)
{
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Alert ('function: changetwodecimal-> parameter error ');
Return false;
}
F_x = math. Round (F_x* 100)/100;
VaR s_x = f_x.tostring ();
VaR pos_decimal = s_x.indexof ('.');
If (pos_decimal <0)
{
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 2)
{
S_x + = '0 ';
}
Return s_x;
}

Function: rounds a floating point number to two digits after the decimal point. If there are less than two digits, the value is 0. This function returns the string format.

Usage: changetwodecimal (3.1415926) returns 3.14

Changetwodecimal (3.1) returns 3.10

 

In addition:

Parsefloat Method

Returns the floating point number converted from a string.

parseFloat(numString)

RequiredNumstringThe parameter is a string containing floating point numbers.

Description

ParsefloatMethod return andNumstringThe number that is saved in the bucket is equal to the number that is saved in the bucket. IfNumstringIf the prefix cannot be interpreted as a floating pointNan(Not a number ).

parseFloat("abc")  // Return NaN.parseFloat("1.2abc")// Return 1.2.

AvailableIsnanMethod detectionNan.

 

 

Example 1:

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT>

Function changettext (){
Document. getelementbyid ("txtb"). value = Document. getelementbyid ("txta"). value;

}

</SCRIPT>

</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txta" runat = "server" onblur = "changettext ()"> </ASP: textbox>
<Asp: textbox id = "txtb" runat = "server"> </ASP: textbox>

</Div>
</Form>
</Body>
</Html>

 

Example 2:

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT>

// Save the value as a three-digit valid value
Function changethreedecimal_f (varobj)
{
VaR x = varobj. value;
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Return;
}
F_x = math. Round (f_x * 1000)/1000;
VaR s_x = f_x.tostring ();
VaR pos_decimal = s_x.indexof ('.');
If (pos_decimal <0)
{
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 3)
{
S_x + = '0 ';
}
Varobj. value = s_x;
}

</SCRIPT>

</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txta" runat = "server" onblur = "changethreedecimal_f (this)"> </ASP: textbox>
<Asp: textbox id = "txtb" runat = "server"> </ASP: textbox>

</Div>
</Form>
</Body>
</Html>

JS retains two decimal places

 

For floating point numbers with multiple digits after decimal points, we may only need to retain two digits, but JS does not provide such a direct function. Therefore, we have to write the function to implement this function by ourselves. The Code is as follows:

Function changetwodecimal (X)
{
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Alert ('function: changetwodecimal-> parameter error ');
Return false;
}
F_x = math. Round (F_x* 100)/100;

Return f_x;
}

Function: rounds a floating point number to the second place after the decimal point.

Usage: changetwodecimal (3.1415926) returns 3.14

Changetwodecimal (3.1475926) returns 3.15

 

JS retains 2 decimal places (mandatory)

 

If the number of decimal places is greater than 2 digits, it is okay to use the above function, but if it is smaller than 2 digits, for example:

Changetwodecimal (3.1) will return 3.1. If you need a format like 3.10, you need the following function:

 

Function changetwodecimal_f (X)
{
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Alert ('function: changetwodecimal-> parameter error ');
Return false;
}
F_x = math. Round (F_x* 100)/100;
VaR s_x = f_x.tostring ();
VaR pos_decimal = s_x.indexof ('.');
If (pos_decimal <0)
{
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 2)
{
S_x + = '0 ';
}
Return s_x;
}

Function: rounds a floating point number to two digits after the decimal point. If there are less than two digits, the value is 0. This function returns the string format.

Usage: changetwodecimal (3.1415926) returns 3.14

Changetwodecimal (3.1) returns 3.10

 

In addition:

Parsefloat Method

Returns the floating point number converted from a string.

parseFloat(numString)

RequiredNumstringThe parameter is a string containing floating point numbers.

Description

ParsefloatMethod return andNumstringThe number that is saved in the bucket is equal to the number that is saved in the bucket. IfNumstringIf the prefix cannot be interpreted as a floating pointNan(Not a number ).

parseFloat("abc")  // Return NaN.parseFloat("1.2abc")// Return 1.2.

AvailableIsnanMethod detectionNan.

 

 

Example 1:

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT>

Function changettext (){
Document. getelementbyid ("txtb"). value = Document. getelementbyid ("txta"). value;

}

</SCRIPT>

</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txta" runat = "server" onblur = "changettext ()"> </ASP: textbox>
<Asp: textbox id = "txtb" runat = "server"> </ASP: textbox>

</Div>
</Form>
</Body>
</Html>

 

Example 2:

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT>

// Save the value as a three-digit valid value
Function changethreedecimal_f (varobj)
{
VaR x = varobj. value;
VaR f_x = parsefloat (X );
If (isnan (f_x ))
{
Return;
}
F_x = math. Round (f_x * 1000)/1000;
VaR s_x = f_x.tostring ();
VaR pos_decimal = s_x.indexof ('.');
If (pos_decimal <0)
{
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 3)
{
S_x + = '0 ';
}
Varobj. value = s_x;
}

</SCRIPT>

</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txta" runat = "server" onblur = "changethreedecimal_f (this)"> </ASP: textbox>
<Asp: textbox id = "txtb" runat = "server"> </ASP: textbox>

</Div>
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.