money_format appears to work. Puts the sign before the $ sign.
setlocale(LC_MONETARY,"en_US");
$money_result = money_format( "%n", $numval ) ;
<?php
header("Content-type: text/html; charset:utf-8;");
?>
<head>
<link rel='stylesheet' type='text/css'
href='
https://code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css'>
<link rel="stylesheet"
href="
https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity=
"sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<script type="text/javascript"
src="
https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript"
src="
https://code.jquery.com/ui/1.11.4/jquery-ui.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
});
</script>
</head>
<?php
$numval = isset($_POST["numval"]) ? $_POST["numval"]: '0' ;
if ( strlen($numval) == 0 )
$numval = '0' ;
$edit_result = isset($_POST["edit_result"]) ? $_POST["edit_result"]: '' ;
$money_result = isset($_POST["money_result"]) ? $_POST["money_result"]: ''
;
$do_edit = isset($_POST["do_edit"]) ? $_POST["do_edit"]: 'off' ;
if (($do_edit == 'on') && ($numval != 0 ))
{
$edit_result = number_format( $numval, 2 ) ;
setlocale(LC_MONETARY,"en_US");
$money_result = money_format( "%n", $numval ) ;
}
?>
<body>
<div class="container">
<h1>edit numeric value</h1>
</div>
<div class="container mt-3">
<form action="submit-form.php" method="POST">
<div class="form-row">
<div class="col-auto">
<div class="form-group">
<label for="numval">Numeric value</label>
<input type="text" class="form-control" id="numval" name="numval"
value="<?= $numval ?>" style="width:8em;"
placeholder="Numberic value">
</div>
</div>
<div class="col-auto">
<div class="form-group">
<label for="edit_result">Edit result</label>
<input type="text" class="form-control" id="edit_result" name=
"edit_result"
value="<?= $edit_result ?>"
title="edit result."
placeholder="edit result">
<hr>
<label for="money_result">Money result</label>
<input type="text" class="form-control" id="money_result" name=
"money_result"
value="<?= $money_result ?>"
title="money result."
placeholder="money result">
</div>
</div>
<div class="col-auto">
<div class="form-group">
<label for="do_edit">Apply edit mask</label>
<input type="checkbox" class="form-control" id="do_edit"
title="apply edit mask to numeric value."
<?= ($do_edit == 'on' ? 'checked' : '' ) ?>
name="do_edit" >
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<?php
echo '<div class="container">' ;
echo 'numval:' . $numval . '<br>' ;
echo 'edit_result: ' . $edit_result . '<br>' ;
echo 'do edit: ' . $do_edit . '<br>' ;
?>
<script src="
https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"
integrity=
"sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T"
crossorigin="anonymous"></script>
</body>
On Tue, Sep 10, 2019 at 7:36 PM Don Brown <DBrown@xxxxxxxxxx> wrote:
I have had a request to format negative dollar values as -$123.45
I have been using the PHO number_format as in the following code fragment;
$" . number_format(AMOUNT, 2)
This provides a result of $-123.45
I thought this would be easy ... but google and w3schools have not helped
... yet
Any suggestions ?
Thank you
Don Brown
______________________________________________________________________
This email has been scanned for computer viruses. Although MSD has taken
reasonable precautions to ensure no viruses are present in this email, MSD
cannot accept responsibility for any loss or damage arising from the use of
this email or attachments.
______________________________________________________________________
--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing
list
To post a message email: WEB400@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/web400.
As an Amazon Associate we earn from qualifying purchases.