Three digits are separated by commas (,).

Source: Internet
Author: User

When a product is released to a customer for use, the customer always asks "we need to separate all the data by commas ". This requirement is not too much at all, because when the number reaches 0.1 million or above, we cannot tell at a glance what the data is.
So we need a friendly digital display method, and we need to do
1. convert a common number to a three-digit representation.
1> we need data in all lists;
2> all data in the text box must be represented by three digits;
3> at the front-end, when entering the value of the text box, the display mode is automatically converted to three-digit display;
2. Convert the three-digit data to normal data without commas (, because, if we need to calculate it based on the value of the existing text box ).
1> convert the three-digit display numbers to numbers without commas in the background;
2> convert the three digits displayed on the front-end to those without commas;

1. convert a common number to a three-digit representation.
1> we need data in all lists;
Gridview: Add dataformatstring = "{0: n}" to the required Column }"
Repeater: <% # string. Format ("{0: n}", Eval ("expense_amount") %>
2> all data in the text box must be represented by three digits;
The following method needs to be put in a public class for type conversion (it is better to have a type conversion class in the project, because the data retrieved from the database, you cannot determine whether it has a value ). In fact, only the double to three represents only one sentence: string. format ("{0: n}", doublevalue), but many cases are from the database, so a lot of judgment is added;
/// <Summary>
/// Convert a numeric string to a formatted string
/// </Summary>
/// <Param name = "strvalue"> string to be converted </param>
/// <Returns> formatted string </returns>
Public static string doublestrtoformat (string strvalue)
{
Return string. Format ("{0: n}", strtodouble (strvalue ));
}

/// <Summary>
/// Convert string type to double type
/// </Summary>
/// <Param name = "strvalue"> string to be converted </param>
/// <Returns> converted int type result </returns>
Public static double strtodouble (string strvalue)
{
If (strvalue = NULL ))
Return 0;

return strtodouble (strvalue. tostring (), 0);
}

/// <Summary>
/// Convert string type to double type
/// </Summary>
/// <Param name = "strvalue"> string to be converted </param>
/// <Param name = "defvalue"> default value </param>
/// <Returns> converted int type result </returns>
Public static double strtodouble (string strvalue, double defvalue)
{
If (strvalue = NULL)
Return defvalue;

Double intvalue = defvalue;
If (strvalue! = NULL)
{
Bool isfloat = RegEx. ismatch (strvalue, @ "^ ([-] | [0-9]) [0-9] * (\. \ W *)? $ ");
If (isfloat)
Double. tryparse (strvalue, out intvalue );
}
Return intvalue;
}
3> at the front-end, when you enter the value of the text box, it is automatically converted to three-node display mode. The following JS can be placed in a public file;
// When the text box value changes, format the number
$ ("Input. formatnum"). Live ("keyup", function (){
VaR objthis = $ (this );
VaR orginlength = objthis. Val (). length; // original character Length
VaR carepos = getcursortposition (objthis [0]);
VaR ret = formatnumber ($. Trim (objthis. Val (), 2 );
Objthis. Val (RET );
VaR newlength = objthis. Val (). length; // new character Length
If (orginlength-newlength> 0) {// When deleting data, if one comma is missing after formatting, the cursor is reduced by 1
Carepos --;
}
If (newlength-orginlength> 0) {// when entering data, add a new comma after formatting, add the cursor to 1
Carepos ++;
}
Setcaretposition (objthis [0], carepos );
});

// Convert the number to a three-digit representation (this method comes from the pcajax blog garden http://www.cnblogs.com/pcajax/archive/2011/03/07/1973369.html)
Function formatnumber (S, N ){
If (S = "" | S = undefined | S = NULL ){
Return "";
}
N = n> 0 & n <= 20? N: 2;
S = parsefloat (S + ""). Replace (/[^ \ D \.-]/g, ""). tofixed (n) + "";
VaR L = S. Split (".") [0]. Split (""). Reverse (),
R = S. Split (".") [1];
T = "";
For (I = 0; I <L. length; I ++ ){
T + = L [I] + (I + 1) % 3 = 0 & (I + 1 )! = L. length? ",":"");
}
Return T. Split (""). Reverse (). Join ("") + "." + R;
}
// Obtain the cursor position function (this method comes from the Internet and cannot find the original author. Sorry)
Function getcursortposition (CTRL ){
VaR caretpos = 0; // ie support
If (document. Selection ){
CTRL. Focus ();
VaR sel = Document. selection. createRange ();
Sel. movestart ('character ',-ctrl. value. Length );
Caretpos = SEL. Text. length;
}
// Firefox support
Else if (CTRL. selectionstart | ctrl. selectionstart = '0 ')
Caretpos = ctrl. selectionstart;
Return (caretpos );
}
// Set the cursor position function (this method is from the Internet and the original author cannot be found. Sorry)
Function setcaretposition (CTRL, POS ){
If (CTRL. setselectionrange ){
CTRL. Focus ();
CTRL. setselectionrange (Pos, POS );
}
Else if (CTRL. createTextRange ){
VaR range = ctrl. createTextRange ();
Range. Collapse (true );
Range. moveend ('character ', POS );
Range. movestart ('character ', POS );
Range. Select ();
}
}
2. Convert the three-digit data to normal data without commas (, because, if we need to calculate it based on the value of the existing text box ).
1> convert the three-digit display numbers to numbers without commas in the background;
/// <Summary>
/// Remove the comma from the string and convert it to the double type.
/// </Summary>
/// <Param name = "strvalue"> string to be converted </param>
/// <Returns> </returns>
Public static double formatstrtodouble (string strvalue)
{
If (strvalue = NULL ))
Return 0;
Return strtodouble (strvalue. Replace (",", ""), 0 );
}
For other methods called, see Code
2> convert the three digits displayed on the front-end to those without commas;
Place the following Js in a public JS File
String. Prototype. replacecomma = function (){
Return this. Replace (",", "") * 1;
}
Eg: var ret = "123,222.21". replacecomma ();

 

Finally, I wanted to create a demo, but vs was suspended and re-installed. I will attach the demo later.

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.