# Display the number with floating point without rounding in JavaScript

Sometime you need to display the result with floating points of the some mathematical calculation without rounding off such as you have the value 12.56667, you want to display 12.56. This is useful when you are playing with Currency.
The following code snippets can be use to truncate values without rounding.

## Method 1: Use Math.trunc(x)

let result = Math.trunc(number*100)/100

We are using 100 because we want to truncate number by 2 decimal points, similarly if you want to truncate number by 3 decimal points you can use the above line as:

let result = Math.trunc(number*1000)/1000

The Complete function in more generic way:

```function truncNumber(number, decimal){
return Math.trunc(number*Math.pow(10, decimal))/Math.pow(10, decimal)
}
// You can use this function like this
console.log(truncNumber(12.66785, 2))
// output will be 12.66```

`Math.trunc()` truncates (cuts off) the dot and the digits to the right of any number. The given number can be positive or negative number.

## Method 2: Using regular expression

You can also use the regx to truncate the number like the following:

```function truncNumber(num, decimal) {
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (decimal|| -1) + '})?');
return Number(num.toString().match(re));
}```

The above code is using simple regular expression which find the string part with desired decimal points. `num.toString().match(re)` returns value as string, If you want the value as number you need to convert into number using Number or parseFloat methods.
{"ft_lang_label":"__label__en","ft_lang_prob":0.65446633,"math_prob":0.9978451,"size":1491,"snap":"2023-40-2023-50","text_gpt3_token_len":356,"char_repetition_ratio":0.13987894,"word_repetition_ratio":0.009090909,"special_character_ratio":0.26760563,"punctuation_ratio":0.14617941,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9991063,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-29T16:32:36Z\",\"WARC-Record-ID\":\"<urn:uuid:2ccd3123-85d6-4aa5-bbbe-078dbc71021a>\",\"Content-Length\":\"47313\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b2cb0109-192a-490a-b3ac-3e37d4c9ae43>\",\"WARC-Concurrent-To\":\"<urn:uuid:55b35440-a877-40b1-9456-ac9aa1a4b0cb>\",\"WARC-IP-Address\":\"85.187.142.69\",\"WARC-Target-URI\":\"https://www.authorcode.com/display-the-number-with-floating-point-without-rounding-in-javascript/\",\"WARC-Payload-Digest\":\"sha1:3VSS4Y66UIM5UOTRAGZ64IP6RTLMQIG7\",\"WARC-Block-Digest\":\"sha1:MUKDA63DV3MDGFYYORQPWP3D3AICQ34Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510520.98_warc_CC-MAIN-20230929154432-20230929184432-00369.warc.gz\"}"}