| |

VerySource

 Forgot password?
 Register
Search
View: 1039|Reply: 5

How to convert the string type to floating point or double precision and calculate it? String To Double / Float

[Copy link]

2

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-26 23:00:01
| Show all posts |Read mode
Excuse me:

How to convert the string type to floating point or double precision and calculate it?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-2-19 12:30:01
| Show all posts
String s = "123.456";
double d = Double.parseDouble (s);
float f = Float.parseFloat (s);
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-4-12 03:00:01
| Show all posts
Double.parseDouble (String str);
Float.parseFloat (String str);
Reply

Use magic Report

0

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-13 18:45:01
| Show all posts
It is best to put the conversion code in the try, so that the String value is not a numeric value and an exception is reported!
try
{
    double d = Double.parseDouble (s);
    float f = Float.parseFloat (s);
}
catch (Exception e)
{
    
}
Reply

Use magic Report

2

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-8-21 11:15:01
| Show all posts
I want JSP methods, none of these methods are in JSP
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 22:30:01
| Show all posts
Talk to me privately, because I want to do a project, I have a full set of calculation methods. Addition, subtraction, multiplication, division and remainder are all
Here is the code:

//According to the problem of loss of precision in addition, subtraction and remainder in floating-point calculations in js add by chengang006 at 2011-10-01
/*
* Since many times, algorithms for floating-point decimals are designed,
* In JS, only using ordinary parseFlost and the like into the envelope data type conversion will make the data lose precision
* Therefore, the method of converting to integers and then calculating is adopted
* */
//Add floating point numbers

function dcmAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}
    try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}
    m=Math.pow(10,Math.max(r1,r2));
    return (accMul(arg1,m)+accMul(arg2,m))/m;
}

//Floating point subtraction add by chengang006 on 2011-10-01
/*
* The description is the same as the above addition
* */
function dcmSub(arg1,arg2){
     return dcmAdd(arg1,-arg2);
}

//Floating point number takes remainder --add by chengang006 on 2011-10-02
/*
*According to actual cases, it is easy to lose accuracy. The usual practice is to expand by 10,000 times at the same time, but consider
* It's related to the previous, so we still use the first round and then calculate
* */
function dcmYu(arg1,arg2){
var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}
    try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}
    m=Math.pow(10,Math.max(r1,r2));
    return (accMul(arg1,m)%accMul(arg2,m))/m;
}
//For the accuracy of floating-point calculations in JavaScript, add by pengym at 2011-08-18

//Division function, used to get the exact division result
//Note: There will be errors in the division result of javascript, which will be more obvious when dividing two floating-point numbers. This function returns a more accurate division result.
//Call: accDiv(arg1,arg2)
//Return value: the exact result of dividing arg1 by arg2
function accDiv(arg1,arg2){
    var t1=0,t2=0,r1,r2;
    try{t1=arg1.toString().split(".")[1].length}catch(e){}
    try{t2=arg2.toString().split(".")[1].length}catch(e){}
    with(Math){
        r1=Number(arg1.toString().replace(".",""))
        r2=Number(arg2.toString().replace(".",""))
        return (r1/r2)*pow(10,t2-t1);
    }
}
//Multiplication function, used to get accurate multiplication result
//Note: There will be errors in the multiplication result of javascript, which will be more obvious when multiplying two floating-point numbers. This function returns a more accurate multiplication result.
//Call: accMul(arg1,arg2)
//Return value: the exact result of arg1 multiplied by arg2
function accMul(arg1,arg2){
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}


// Converted to decimals, the original function toDecimal(datavalue) has precision problems, because it involves too much masking.
function toDecimal(datevalue){
if(datevalue.indexOf('%') != -1){
datevalue = datevalue.replace(/%/g,'');
      if(datevalue.indexOf(',') != -1) {
      datevalue = datevalue.replace(/,/g,'');
      }
      // The precision of division by 100 is increased by 2 digits on the original basis.
var decimal = (datevalue.indexOf('.') == -1)? 0: (datevalue.length-datevalue.indexOf('.')-1);
      datevalue = accDiv(datevalue, 100).toFixed(decimal + 2);
// alert("toDecimal: "+ datevalue);
    } else {
if(datevalue.indexOf(',') != -1){
datevalue = datevalue.replace(/,/g,'');
      }
    }
    return datevalue;
}

// Convert decimals to percentages.
function toPercentFormat(datevalue) {
var aa = accMul(datevalue, 100);
return "" + aa + "%";
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list