Procter & Gamble swung to a net loss in its fiscal fourth quarter after the company reported an $8 billion write down of its Gillette brand.

But P&G shares rallied Tuesday, as earnings topped expectations after stripping out the impact of the charge, and the company offered up an optimistic forecast for fiscal 2020. P&G shares, which have climbed nearly 44% over the past year, hit an all-time high and were trading up 4%.

Here's what the company reported compared with what Wall Street was expecting, based on a survey of analysts by Refinitiv:

Earnings per share: $1.10, adjusted, vs. $1.05 expected

Revenue: $17.09 billion vs. $16.86 billion expected

The consumer products giant reported a fiscal fourth-quarter net loss of $5.24 billion, or $2.12 per share, compared with net income of $1.89 billion, or 72 cents per share, a year earlier. The primary driver of the loss during the quarter ended June 30 was the one-time charge to write down the value of Gillette.

Excluding items, P&G earned $1.10 per share, beating the $1.05 per share expected by analysts surveyed by Refinitiv.

Net sales rose 4% to $17.09 billion, topping expectations of $16.86 billion.

P&G said that currency devaluations and market contraction of blades and razors, primarily in developed markets, led the company to adjust the carrying values of goodwill and intangible assets for the business. Gillette has struggled as more men grow beards or turn to competitors like Unilever's Dollar Shave Club or Harry's.

Its grooming segment, which also includes Braun and the Art of Shaving brands, once again saw shipment volume decline during the quarter. Its organic sales, which strips out the impact of foreign currency, acquisitions and divestitures, turned positive thanks to price increases and higher sales in developed regions.

Organic sales increased by 7% during the quarter, helped by price hikes and more consumers buying products from brands like Tide and Swiffer. CEO David Taylor said in a statement that it was its strongest quarter of organic sales in over a decade. Global e-commerce organic sales now make up 8% of the company's total.

The company has been trying to grow sales by using consumer data to understand what products to create next and speeding up how quickly it brings a new product to market.

"Market shares have been improving for eight consecutive quarters," Taylor said on the conference call.

Its health-care and fabric and home-care businesses were its two strongest performers. P&G's beauty segment, which includes premium beauty line SK-II and Olay, also performed well.

Its health care business, which includes Pepto-Bismol and Crest, saw organic sales jump 10%. A late season increase in cough and colds contributed to higher sales for brands like Vicks and ZzzQuil.

P&G's fabric and home-care segment, which includes its laundry and dishwasher detergent brands, also reported organic sales growth of 10% in the quarter. Executives attributed the growth to its efforts to expand the fabric-care market by releasing higher priced products like Tide Pods and Downy scented beads.

The company said it expects fiscal 2020 revenue growth in the range of 3% to 4%. This includes a slight negative impact from foreign currency. Wall Street was forecasting fiscal 2020 revenue of $69.76 billion, up 3.5% from fiscal 2019.

"Our guidance range brackets current market growth with a bias toward continued share growth, while still expecting a strong competitive response," CFO Jon Moeller told analysts on the call.

It also expects adjusted earnings per share to increase by 4% to 9%. P&G said that its current forecast for commodities, foreign currency, transportation and tariffs is expected to result in a "modest net benefit" to earnings growth in fiscal 2020. Analysts were estimating that the company's adjusted earnings next fiscal year would rise 5.1% to $4.75 per share.