#Pipe for getting 2 values after decimal point

16 messages · Page 1 of 1 (latest)

upper cosmos
#

I have created a custom pipe to get only 2 values after decimal point for amount.

let beforedecimal = value.split('.')[0];
let afterdecimal = value.split('.')[1];
if (afterdecimal.length < 2)
{
while (afterdecimal.length < 2) {
afterdecimal = afterdecimal+'0'
}
}
else if (afterdecimal.length > 2)
{
afterdecimal= afterdecimal.substring(0,1);
}
result = (beforedecimal+'.'+afterdecimal).trim();
return result;

This is working fine for amount having values after decimal point but getting undefined if there is nothing after the decimal point i.e., 56000.00 for this amount getting blank in UI screen.

Also, I want commas at appropriate places as this is amount i.e., 56,000.00

Thanks

fickle sun
upper cosmos
#

Yes, my first approach was this but there was one particular type of data which was not taking this pipe it was only showing 1 value after decimal point so someone suggested that a custom pipe could work and it did but it's failing for this condition

fickle sun
amber storm
#

Did you try to use the digitsInfo of the DecimalPipe ? like this {{ 56000.00 | number: '1.2-2' }}

#

{{ 56000.00 | number: '1.2-2' }} --> 56,000.00

upper cosmos
#

I did. As I said, this approach was working perfectly for every instance except one so to fix that I had to create this custom pipe

amber storm
#

Could you please write the case in error ?

upper cosmos
#

No error, just for 1 type of data I was getting only one value after decimal point only one case in thousands not sure why, I had done exactly the way you have done in the picture and it was working for every other except this one case for which I had to create custom pipe

fickle sun
#

Could you post that problematic case?

upper cosmos
#

It's not feasible and also is specific to my organisation

#

If you could just help me with the condition if I get 00 after decimal point

fickle sun
#

I don't know how a number can be specific to your organization.
And still can't realize how it can give a problem. My suggestion is still to present an equivalent case.
I think your solution cannot be business reliable, because working with strings and indexes requires to take in account many corner cases. That's why relying on strong and tested professional solutions like native angular pipes is advisable.
As a side note, I hope the numbers you're talking about do not represent currencies, because in that case you are totally on the wrong path.

fickle sun
#

I just noticed that your value can even be shorter than expected format.
So I deleted my oneliner a suggest this instead:

if ( value.indexOf('.') === -1) {
     value = value.concat('.00')
} else if (value.length - value.indexOf('.') > 3) {
    value = value.substring(0, value.indexOf('.')+3)
} else {
    while (value.length - value.indexOf('.') < 3) {
        value = value.concat('0')
    }
}

My previous warnings stand still, tho.

upper cosmos
#

It does represent, currency values are in dollars that's why the second part of my question was to get commas at the right places