PHP编号:将1.4E-N(N> 40)转换为十进制,删除无用的零位数

时间:2018-04-25 03:25:06

标签: php

我从第三方服务获得非常小的值,我需要将其转换为删除无用数字的数字。  `

<?php 
    $decimal = 50 
    //If value is 1.4E-45
    number_format($value, $decimal); 
    //output: 0.00000000000000000000000000000000000000000000140000 
    number_format($value, $decimal) +0 ;
    //output: 1.4E-45 
?>

`

示例输入::

  • 1

    输出1

  • 1.00000000000000000000000000000000000000000000000000

    输出:1

  • 1.4E-45

    输出:0.0000000000000000000000000000000000000000000014

正如您在代码中看到的,已经尝试添加0和number_format方法。

可以有两种方法

  1. 发现$ decimal值需要动态使用
  2. 始终传递$ decimal的最大可能值并删除无用的0。 我们可以假设$ decimal的上限为50.
  3. 请建议php函数中的任何方法是否适用于任何方法

2 个答案:

答案 0 :(得分:2)

我推荐这种方法:

import thunk from 'redux-thunk';
import { persistStore } from 'redux-persist';
import { History, createBrowserHistory } from 'history';
import { createUserManager, loadUser } from "redux-oidc";
import { routerReducer, routerMiddleware } from 'react-router-redux';
import { createStore, applyMiddleware, compose, combineReducers, GenericStoreEnhancer, Store, StoreEnhancerStoreCreator, ReducersMapObject } from 'redux';

import * as StoreModule from './reducers';
import { ApplicationState, reducers } from './reducers';

import userManager from "./utils/userManager";

// Create browser history to use in the Redux store
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
const history = createBrowserHistory({ basename: baseUrl });

export default function configureStore(history: History, initialState?: ApplicationState) {
    // Build middleware. These are functions that can process the actions before they reach the store.
    const windowIfDefined = typeof window === 'undefined' ? null : window as any;
    // If devTools is installed, connect to it
    const devToolsExtension = windowIfDefined && windowIfDefined.__REDUX_DEVTOOLS_EXTENSION__ as () => GenericStoreEnhancer;


    const createStoreWithMiddleware = compose(
        applyMiddleware(thunk, routerMiddleware(history)),
        devToolsExtension ? devToolsExtension() : <S>(next: StoreEnhancerStoreCreator<S>) => next
    )(createStore);

    // Combine all reducers and instantiate the app-wide store instance
    const allReducers = buildRootReducer(reducers);
    const store = createStoreWithMiddleware(allReducers, initialState) as Store<ApplicationState>;
    loadUser(store, userManager);

    // Enable Webpack hot module replacement for reducers
    if (module.hot) {
        module.hot.accept('./reducers', () => {
            const nextRootReducer = require<typeof StoreModule>('./reducers');
            store.replaceReducer(buildRootReducer(nextRootReducer.reducers));
        });
    }
    const persistor = persistStore(store);
    return { store, persistor };
}

function buildRootReducer(allReducers: ReducersMapObject) {
    return combineReducers<ApplicationState>(Object.assign({}, allReducers, { routing: routerReducer }));
}

// Get the application-wide store instance, prepopulating with state from the server where available.
const initialState = (window as any).initialReduxState as ApplicationState;
export const { store, persistor } = configureStore(history, initialState);

如果您不注意边缘情况下的精确度损失并看到一些科学记数法,那么或者将您的字符串转换为浮动字符串。精心使用的rtrim可能是最好的方法。

答案 1 :(得分:1)

您将小数设置为50,这就是它输出最后一个零的原因 如果你计算$ decimals应该是什么,那么它应该给你你需要的东西。

ComponentTest

https://3v4l.org/fE1XD

它很漂亮,但它适用于此。

编辑:我有数字格式($ value, 100 )因为我需要确保将完整数字作为浮动值。
然后我使用preg_match来查找模式[0]以及你的数字在这个浮点数上的位置。

编辑2。 这将向您展示为什么固定小数不是一个好的解决方案 浮动数字不准确 https://3v4l.org/Ai0kE