webrobert's avatar

Number Format: does it really take this many lines?

to get this...

Total Purchase Price: One Hundred Ninety Thousand United States Dollars, ($190,000.00 USD)

Feeling a bit bloated?

$purchase_price = 190000;

$number = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$spellOut = new NumberFormatter("en_US", NumberFormatter::SPELLOUT);
$price_spelled_out = Str::Headline($spellOut->format($purchase_price));
$price_formatted = $number->formatCurrency($purchase_price, 'USD');
{{ $price_spelled_out }} United States Dollars, ({{ $price_formatted }} USD),
0 likes
6 replies
kokoshneta's avatar
Level 27

That doesn’t seem bloated to me… it’s highly efficient to be able to convert an integer into a grammatically valid noun phrase and an economically valid thousand-separated currency value in just four lines of code.

I have a base Price class that I use which abstracts away most of the need to pass locales, currencies, formatters and such things, instead getting them straight from the app/config/session (unless explicitly passed in the function call), converting the amount to the appropriate currency if necessary, etc. Using that, I could get the output done more easily:

$price = new Price(190000);

// Blade
{{ $price->asWords() }} United States Dollars ({{ $price->asCurrency() }})

But the actual class itself uses about a hundred lines to achieve this, so it’s only less ‘bloat’ in the controller and view, not overall.

2 likes
webrobert's avatar

I like this...

{{ $price->asWords() }} United States Dollars ({{ $price->asCurrency() }})

I just added the class Price.

Thank you @kokoshneta, @lbecket and @mohamedtammam.

This is for a new feature I'm building, it's part of our contracts doc set. I'm going to make a new question.. (for packaging documents). I am going to build and send them to docusign for signatures. Tired of word docs and having party data spread all over the place and waiting for people to sign and scan.

If you guys have any insight on the topic. stay tuned... ha.

Thanks again.

1 like
kokoshneta's avatar

@webrobert That sounds to me a bit like reinventing the wheel. Is there any reason you can’t use something like Penneo?

webrobert's avatar

@kokoshneta,

Thanks for the reply. Yes, it probably would be reinventing the wheel. It's why I am asking the question. There is probably a clever service for this that i'm not aware of. Penneo looks like it could be interesting.. doesn't appear to work in the US.

I'd say the only constraint is cost. A bit of context. This is a small business that buys businesses.. At present we'll do probably a dozen signings. in the next 12 months. So it's not a lot. Even docusign, they require a minimum spend to use their api. Its silly, but we're in the age of monthly subscriptions. They ADD up quickly.

So just balancing that. But yeah, I think there are some great signing providers I just need to build the file. OR find one that has that capability. Where we can upload the contract like a word doc.. like blade and drop in the variables {{ template }} vs laying them over a pdf.

fyi, I did make another thread.

MohamedTammam's avatar

I also agree with others here. That's look very good. Just 4 lines to get that information. With function is better.

function getMoneySpellAndFormat($number) {
	$number = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
	$spellOut = new NumberFormatter("en_US", NumberFormatter::SPELLOUT);
	$price_spelled_out = Str::Headline($spellOut->format($purchase_price));
	$price_formatted = $number->formatCurrency($purchase_price, 'USD');
	retrurn [$price_spelled_out, $price_formatted ];
}
// Usage
[$spell, $formate] = getMoneySpellAndFormat(190000);
1 like

Please or to participate in this conversation.