vc arbitrary bit string, said the figures rounded to the nearest decimal places to retain and

BOOL SHGlobal::isIntOrDecimal(CString strItem)
{
        //  Determine whether an integer or decimal
        //  Returns 0 for not
        //  Returns the integer 1 representative
        //  Returns 2 represents decimal
        if(strItem.IsEmpty())
                return FALSE;

        for(int i = 0; i <strItem.GetLength(); i++)
        {
                //   check "+ ". "- "
                if(i == 0 && (strItem.GetAt(i) == 0x2B || strItem.GetAt(i) == 0x2D))
                        continue; 

                //   check   char
                if( !isdigit(strItem.GetAt(i)) && strItem.GetAt(i) != '.')
                        return   FALSE;
    } 

    //   check    Decimal point
    if(strItem.Find( '.') != strItem.ReverseFind( '.'))
        return FALSE; 

      return TRUE;
}

// tS  A string that represents the manipulation
// tDecimalCount  Indicates the need to retain the number of decimal places
// tRound  Indicates whether the rounding operation
// addLenWhenLarger  Represents were rounded exceeds the current length of the range of digits that are added
CString SHGlobal::strRound(CString tS, int tDecimalCount, boolean tRound, boolean addLenWhenLarger)
{
        if(!isIntOrDecimal(tS))
                AfxMessageBox(_T(" Illegal numbers cannot be formatted  "));

        int strLen = tS.GetLength();
        int dotIndex = tS.Find('.');    //  The location of the decimal point
        int i = 0 ;

        if(dotIndex < 0 && tDecimalCount <= 0)//  The integer itself and does not require decimal places
        {}
        else if(dotIndex < 0 && tDecimalCount > 0)        //  Itself is an integer, but the need to increase the number of decimal places
        {
                tS += ".";
                for(i =0 ; i < tDecimalCount; i++)
                        tS += "0";
        }else if(dotIndex > 0 && dotIndex + tDecimalCount + 1 >= strLen)
                //  Itself a small number of decimal digits but is less than or equal to the number of decimal places you want to keep
        {
                int tDots = (dotIndex + tDecimalCount + 1) - strLen;
                for(i =0 ; i < tDots; i++)
                        tS += "0";
        }else if(dotIndex > 0 && dotIndex + tDecimalCount + 1 < strLen){
                //  Itself a decimal and decimal is greater than the number of decimal places you want to keep, you need to format
                if(!tRound){
                        //  You do not need to round up
                        tS = tS.Mid(0, dotIndex + tDecimalCount + 1);
                }
                else{
                        //  You need to round up
                        if(tS.GetAt(dotIndex + tDecimalCount + 1) < '5'){
                                //  Keep the number of decimal places after a bit of less than five
                                tS = tS.Mid(0, dotIndex + tDecimalCount + 1);
                        }else{
                                //  From the last bit forward to carry judgment
                                tS = tS.Mid(0, dotIndex + tDecimalCount + 1);
                                boolean needtoAdd = TRUE;       //  Is there a carry
                                for(i = tS.GetLength() - 1; i >=0 ; i--){
                                        if(needtoAdd){
                                                char tCC = tS.GetAt(i);
                                                if(tCC == '.')
                                                        continue;
                                                tCC++;
                                                if(tCC > '9'){
                                                        //  Produce a carry
                                                        tCC = '0';
                                                        tS.SetAt(i,tCC);
                                                        needtoAdd = TRUE;
                                                }else {
                                                        needtoAdd = FALSE;
                                                        tS.SetAt(i,tCC);
                                                        break;
                                                }
                                        }
                                }
                                if(needtoAdd && addLenWhenLarger){
                                        //  When the first also need when rounding up and allows when rounding up
                                        tS = "1" + tS;
                                }
                        }
                }
        }

        return tS;

}
分类:CPP 时间:2010-06-03 人气:196
分享到:
blog comments powered by Disqus

相关文章

  • in javascript how to get only two decimal places? 2009-08-16

    Cases sum = parseFloat (8.99) + parseFloat (7.50); Wait until the value is: 16.4900000002 How to get only two decimal places? <script language="JScript"> Number.prototype.toFixed = function (num) ( / / Re-structure toFixed method, IE5.0 +

  • to retain two decimal places java 2010-03-29

    to retain two decimal places java questions: Mode 1: Rounding double f = 111231.5585; BigDecimal b = new BigDecimal (f); double f1 = b.setScale (2, BigDecimal.ROUND_HALF_UP). doubleValue (); Mode 2: java.text.DecimalFormat df = new java.text.DecimalF

  • actionscript how to control the number precision (decimal places) 2010-04-16

    Very simple thing, but if you do not know it as a hassle. Number two bag approach can easily solve, rather than the Math package. Here is the official document: toFixed (3) How to return rounded to three decimal places of the string. var num: Number

  • javascript decimal places for the treatment of retained 2010-10-12

    Transfer from: http://blog.csdn.net/lxh413/archive/2010/02/01/5276584.aspx Often encountered in the development process to adjust the decimal format, such as to retain two decimal places and so on. Method is also quite common, notes below. First, the

  • 2 decimal places retained in ruby 2010-10-23

    ruby's Kernal.sprintf () can be achieved with two decimal places of the method, format is the sprintf's alias. format (pattern, value): pattern, said one form to format, where we use "% .2 f", expressed reservations about the 2 digits after the

  • How to flash decimal places reserved 2010-11-04

    Easier to make money at home no built-in flash in the preservation method of decimal places, but when you want, such as 3.14159265354. so when the data to retain only how to do a double-digit, the following function can solve this problem. / ** * Dat

  • js accurate to several decimal places rounding function 2010-12-26

    can be achieved using the Math.round js integer rounding, if you need to achieve accurate to how many decimal places you need to write a custom function. Method One: function round (v, e) { var t = 1; for (; e> 0; t *= 10, e -); for (; e <0; t / = 1

  • JS float type conversion and take n decimal places 2010-03-27

    javascript variables are weakly typed, all the variables are declared as var, the type conversion process there is no java so convenient, it is through parseInt (variable), parseFloat (variable) and other methods to carry out the type conversion. Not

  • Javascript interception of decimal places and rounded 2010-05-05

    round method Role: to return and give expression to the nearest integer value. Syntax: Math.round (number), will be option number parameter is rounded to the nearest integer value. Note: If the number is greater than equal to the fractional part of 0

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.128 (s). 12 q(s)