0

I currently use this pipe {{ person.ageInDays/7 | number:'1.0-0' }} to show a person's age in week which seems not working accurately! It rounds the number up sometimes.

For example for an infant with 257 days old it should be 36 weeks and and 5 days whereas the result which is 37 weeks and 5 days!

1

2 Answers 2

1

If you see the angular doc, it clearly mentions the behavior of number pipe.

If no parameters are specified, the function rounds off to the nearest value using this rounding method. The behavior differs from that of the JavaScript Math.round() function. In the following case for example, the pipe rounds down where Math.round() rounds up

You would have to create a custom pipe in your case

@Pipe({name: 'daystoweek'})
export class DaysToWeekPipe implements PipeTransform {
    transform(value: number): number {
        return Math.floor(value);
    }
}

Use that custom pipe in your code

{{ numbervalue | daystoweek}}
0

Try Decimal JS

import Decimal from "decimal.js";

let r: Decimal.Rounding = Decimal.ROUND_UP;
let c: Decimal.Configuration = {rounding: r };
Decimal.set(c);
let v: Decimal.Value = "12345.6789";
let d: Decimal = new Decimal(v);

document.getElementById("app").innerHTML = `
  <div>
    ${d.toFixed(3)}
  </div>`

Not the answer you're looking for? Browse other questions tagged or ask your own question.