Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a number has .00 how can you remove it?

I've been looking around for this answer and there are some alternatives. Unfortunately, none of them makes sense to me.

I am working on an e-commerce website where they the product price automatically uploads '.00' if its a whole number. We have some products that will display the decimals where used (i.e £13.50) but we just want all instances of .00 removed from pricing.

Do let me know if anyone needs any more information on this. I know the pricing comes from many parts of the website and not just one particular class.

like image 760
user1943465 Avatar asked Jun 18 '13 12:06

user1943465


People also ask

How do you remove .00 from a string?

str = str. replace(/\. 00([^\d])/g,'$1');

How do you remove decimals from a number?

Step 1: Write down the decimal divided by 1. Step 2: Multiply both top and bottom by 10 for every number after the decimal point. (For example, if there are two numbers after the decimal point, then use 100, if there are three then use 1000, etc.) Step 3: Simplify (or reduce) the Rational number.

How do I get rid of .00 in Python?

Using int() method To remove the decimal from a number, we can use the int() method in Python. The int() method takes the number as an argument and returns the integer by removing the decimal part from it. It can be also used with negative numbers.


1 Answers

If you only want to remove the .00 if it's the end of a string, then you may do

str = str.replace(/\.00$/,'');

If your .00 may not be at the end of your string, for example it's "10.00 $ and 24.00€", then do

str = str.replace(/\.00([^\d])/g,'$1');

What follows here is more a comment :

Most often, number formatting involve many other requirements, we can't guess what are yours.

Here's for example the test set of one of my number formatting functions :

//   formatFloat(100.0)     => "100"
//   formatFloat(100.0, 12) => "100"
//   formatFloat(100.1)    => "100.1"
//   formatFloat(100.1, 0) => "100"
//   formatFloat(100.7, 0) => "101"
//   formatFloat(.0434) => "0.043"
//   formatFloat(1.999999) => "2"
//   formatFloat(.0000047)     => "0"
//   formatFloat(.0000047, 6)  => "0.000005"
//   formatFloat(.0000047, 12) => "0.0000047"
//   formatFloat(undefined) => ""
//   formatFloat(NaN) => ""

So if you're not happy with the solution I gave you, please take the time to define your complete requirement.

like image 111
Denys Séguret Avatar answered Sep 30 '22 15:09

Denys Séguret