@@ -11,6 +11,18 @@ function kilometersToMiles(miles: number) {
1111 return miles * 0.621371 ;
1212}
1313
14+ /**
15+ * Returns the number of decimal places a number has
16+ */
17+ function precision ( num : number ) : number {
18+ const decimalPart = num . toString ( ) . split ( '.' ) [ 1 ] ;
19+ if ( decimalPart === undefined ) {
20+ return 0 ;
21+ }
22+
23+ return decimalPart . length ;
24+ }
25+
1426// http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
1527const EQUATORIAL_EARTH_RADIUS = 6378.137 ;
1628
@@ -239,7 +251,7 @@ describe('location', () => {
239251 const latitude = faker . location . latitude ( { max : 5 , min : - 5 } ) ;
240252
241253 expect (
242- latitude . toString ( ) . split ( '.' ) [ 1 ] . length ,
254+ precision ( latitude ) ,
243255 'The precision of latitude should be 4 digits'
244256 ) . lessThanOrEqual ( 4 ) ;
245257
@@ -251,7 +263,7 @@ describe('location', () => {
251263 const latitude = faker . location . latitude ( { precision : 7 } ) ;
252264
253265 expect (
254- latitude . toString ( ) . split ( '.' ) [ 1 ] . length ,
266+ precision ( latitude ) ,
255267 'The precision of latitude should be 7 digits'
256268 ) . lessThanOrEqual ( 7 ) ;
257269
@@ -278,7 +290,7 @@ describe('location', () => {
278290 const longitude = faker . location . longitude ( { max : 100 , min : - 30 } ) ;
279291
280292 expect (
281- longitude . toString ( ) . split ( '.' ) [ 1 ] . length ,
293+ precision ( longitude ) ,
282294 'The precision of longitude should be 4 digits'
283295 ) . lessThanOrEqual ( 4 ) ;
284296
@@ -290,7 +302,7 @@ describe('location', () => {
290302 const longitude = faker . location . longitude ( { precision : 7 } ) ;
291303
292304 expect (
293- longitude . toString ( ) . split ( '.' ) [ 1 ] . length ,
305+ precision ( longitude ) ,
294306 'The precision of longitude should be 7 digits'
295307 ) . lessThanOrEqual ( 7 ) ;
296308
0 commit comments