fix rounded method two decimal places

This commit is contained in:
Hafifie PKB
2024-03-11 07:25:45 +08:00
parent 640e7291b9
commit 91177ffc0b
3 changed files with 59 additions and 21 deletions
+21
View File
@@ -0,0 +1,21 @@
function getRoundedMethod(number = 0.0) {
const numberString = (Math.floor(number * 100) / 100).toFixed(2);
let rounded = numberString.slice(0, -1);
const lastDigit = parseInt(numberString.slice(-1), 10);
if (lastDigit >= 0 && lastDigit <= 2) {
rounded = rounded.replace(',', ''); // Remove commas
return parseFloat(rounded);
} else if (lastDigit >= 3 && lastDigit <= 7) {
rounded += '5';
rounded = rounded.replace(',', ''); // Remove commas
return parseFloat(rounded);
} else {
return customRound(number);
}
}
function customRound(number) {
const precision = 1; // Round to one decimal place
const factor = Math.pow(10, precision);
return Math.ceil(number * factor) / factor;
}
+33 -17
View File
@@ -76,13 +76,28 @@
'nilaimarhun' => $gadaiannorujukan['nilaimarhun'],
'kodcaw' => $gadaiannorujukan['kodcaw']
])->json();
// if($gadaiannorujukan['nilaimarhun'] > 0 && $gadaiannorujukan['nilaimarhun'] < 401){
// $kadarupah = 1.2/100;
// }else if($gadaiannorujukan['nilaimarhun'] >= 401 && $gadaiannorujukan['nilaimarhun'] < 2001){
// $kadarupah = 1.5/100;
// }else if($gadaiannorujukan['nilaimarhun'] >= 2001 ){
// $kadarupah = 1.7/100;
// }
function getRoundedMethod(float $number = 0.0) : Float{
$number_string = number_format(floor($number * 100)/100,2);
$rounded = substr($number_string, 0, -1);
$lastDigit = substr($number_string, -1);
if ($lastDigit >= 0 && $lastDigit <= 2) {
if(strpos($rounded,',')){
$rounded = str_replace(',','',$rounded);
}
return $rounded;
} elseif ($lastDigit >= 3 && $lastDigit <= 7) {
$rounded = $rounded . '5';
if(strpos($rounded,',')){
$rounded = str_replace(',','',$rounded);
}
return $rounded;
} else {
return round($number,PHP_ROUND_HALF_UP);
}
}
$array_keuntungan = Http::post(config('api.url'). '/api/onepercent',[
'nilaimarhun' => $gadaiannorujukan['nilaimarhun'],
@@ -92,10 +107,10 @@
'kodcaw' => $gadaiannorujukan['kodcaw']
]);
$keuntungan_sebulan = $array_keuntungan['keuntungan'];
$keuntungan_6bln = $keuntungan_sebulan * 6;
$ujrah = $array_keuntungan['ujrah'];
$onepercent = $array_keuntungan['onepercent'];
$keuntungan_sebulan = getRoundedMethod($array_keuntungan['keuntungan']);
$keuntungan_6bln = getRoundedMethod($keuntungan_sebulan * 6);
$ujrah = getRoundedMethod($array_keuntungan['ujrah']);
$onepercent = getRoundedMethod($array_keuntungan['onepercent']);
@endphp
<div class="col-3">
@@ -249,6 +264,7 @@
</div>
</div>
</div>
<script src="{{ asset('js/rounded-methods.js') }}"></script>
<script>
var api_url = "<?php echo $api_url ?>";
var kodcaw_ujrah = "<?php echo $cawangan_info['kodcaw'] ?>";
@@ -477,8 +493,8 @@
}
});
let keuntungan_sebulan = array_keuntungan['keuntungan'];
let keuntungan_6bln = keuntungan_sebulan * 6;
let keuntungan_sebulan = getRoundedMethod(array_keuntungan['keuntungan']);
let keuntungan_6bln = getRoundedMethod(keuntungan_sebulan * 6);
$('.span-pinjaman').text(formatter.format(perpinjam.toFixed(2)) + ' %');
$('#keuntungan_sebulan').val(formatter.format(keuntungan_sebulan.toFixed(2)));
@@ -656,16 +672,16 @@
});
var keuntungan_sebulan = (array_keuntungan['keuntungan']) ? array_keuntungan['keuntungan'] : 0;
var keuntungan_6bln = keuntungan_sebulan * 6;
var keuntungan_sebulan = (array_keuntungan['keuntungan']) ? getRoundedMethod(array_keuntungan['keuntungan']) : 0;
var keuntungan_6bln = getRoundedMethod(keuntungan_sebulan * 6);
percent_pinjaman = (Number(pinjaman)/Number(total_nilai_marhun))*100;
$('.span-pinjaman').text(formatter.format(percent_pinjaman.toFixed(2)) + ' %');
$('#keuntungan_sebulan').val(formatter.format(keuntungan_sebulan.toFixed(2)));
$('#keuntungan_6bln').val(formatter.format(keuntungan_6bln.toFixed(2)));
$('#ujrah').val(formatter.format(array_keuntungan['ujrah'].toFixed(2)));
$('#onepercent').val(formatter.format(array_keuntungan['onepercent'].toFixed(2)));
$('#ujrah').val(formatter.format(getRoundedMethod(array_keuntungan['ujrah']).toFixed(2)));
$('#onepercent').val(formatter.format(getRoundedMethod(array_keuntungan['onepercent']).toFixed(2)));
});
const formatter = new Intl.NumberFormat('en-US', {
+5 -4
View File
@@ -261,6 +261,7 @@
header("Refresh:0");
@endphp
@endif
<script src="{{ asset('js/rounded-methods.js') }}"></script>
<script>
var api_url = "<?php echo $api_url ?>";
var kodcaw_ujrah = "<?php echo $cawangan_info['kodcaw'] ?>";
@@ -491,14 +492,14 @@
}
});
var keuntungan_sebulan = array_keuntungan['keuntungan'];
var keuntungan_6bln = keuntungan_sebulan * 6;
var keuntungan_sebulan = getRoundedMethod(array_keuntungan['keuntungan']);
var keuntungan_6bln = getRoundedMethod(keuntungan_sebulan * 6);
$('.span-pinjaman').text(formatter.format(percent_pinjaman.toFixed(2)) + ' %');
$('#keuntungan_sebulan').val(formatter.format(keuntungan_sebulan.toFixed(2)));
$('#keuntungan_6bln').val(formatter.format(keuntungan_6bln.toFixed(2)));
$('#ujrah').val(formatter.format(array_keuntungan['ujrah'].toFixed(2)));
$('#onepercent').val(formatter.format(array_keuntungan['onepercent'].toFixed(2)));
$('#ujrah').val(formatter.format(getRoundedMethod(array_keuntungan['ujrah']).toFixed(2)));
$('#onepercent').val(formatter.format(getRoundedMethod(array_keuntungan['onepercent']).toFixed(2)));
});