0

Can we use a regular expression to convert a float to int using string? I have been trying a float multiplication and addition but I am stuck at floating point error.

My numbers are between 0.01 and 100.00

I first tried the direct float but it gives me float error https://codepen.io/anurag-desai/pen/QZKpbO

0.1+0.1+0.1 gives me an error of a rounding

So now I am trying another approach to convert all the numbers to integers and then add them so I could skip the entire float errors.

Problem: 0.29*100=28.9999996 instead of 29

So Can I use a regular expression for this?

Like it will remove the decimal places by considering the entire number as a string

ie:

0.01 ==>001

0.1==>010

1==>100

10 ==>1000

So I have this thought but no right way to do it

Step1: Standardise the float

0.01 ==>0.01

0.1==>0.10

1==>1.00

10 ==>10.00

Step 2: Remove the deicmal point.

Step3: Convert to Int and use this as Integer

But I am not able to get the regular expression for standardizing

Can someone help me with this?

Community
  • 1
  • 1
Anurag.Desai
  • 103
  • 1
  • 15
  • 1
    *"Problem: 0.29*100=28.9999996 instead of 29"*: why don't you just round the result, since you know that you only had two decimals in the input? `Math.round( i * 100 )`. – trincot Oct 06 '18 at 09:22
  • 1
    Because math.round in itself uses toFixed which will cause some extent of error. – Anurag.Desai Oct 06 '18 at 09:24
  • 1
    @Anurag.Desai, not if the premise is that the expected result is an integer. OP says original input has at most 2 decimals. NB: `Math.round` does not use `toFixed`. – trincot Oct 06 '18 at 09:27
  • @Andreas I have read the answer that you have pointed. But the approach that I am trying is different that what is suggested there – Anurag.Desai Oct 06 '18 at 09:28
  • Could you provide a concrete example where you have the issue? The codepen just adds 0.01 a lot of times which indeed increases the error, but is that *really* what you want to achieve? Then why not just add 1 repeatedly and only divide by 100 when you need to see the output? That will give better results. – trincot Oct 06 '18 at 09:32
  • @trincot I checked for a library (math.js http://mathjs.org/) the code inside used toFixed and the function was named math.round(). So I took .round performs tofixed internally. And at end I will add the decimal back from the last two places – Anurag.Desai Oct 06 '18 at 09:34
  • I had created another code-pen https://codepen.io/anurag-desai/pen/qJarBz the problem is with the rounding values causes rounding errors at some or the other combinations of values: I am adding the values too – Anurag.Desai Oct 06 '18 at 09:38
  • 1
    I looked at that code. So... why do you calculate `floatValue` and `FloatAdd` as a float? Why not keep working with integers and *only* show the decimal representation when you output (temporarily dividing by 100, but not using that result in any further calculation, always making sure you stick to the integers). – trincot Oct 06 '18 at 09:42
  • I just have the feeling that the using the round or the toFixed is going to give me an error at some or the other combinations of value. Which I don't want to get in so I thought string based regex would be better for the integer conversion and then as you said stick to the integers – Anurag.Desai Oct 06 '18 at 09:48
  • 1
    Using strings and regex is very inefficient. I don't get your point. Just do all your calculations with 100x the intended value, so that you have pure integers to work with and which do not give any error as long as you work with integers < 10^15. *Only* when you display the result, only then divide by 100. There will be no visible problem with inaccuracy then. To be sure you can even apply `toFixed` to that quotient. The internal error will be like 1e-14, so rounding that to just 2 decimal digits with `toFixed` *always* yields what you need. Don't use those floats for any further calculation. – trincot Oct 06 '18 at 10:35

1 Answers1

-1

the bellow code display 29 alert(Math.round(0.29 * 100));

LDS
  • 354
  • 3
  • 9